相关疑难解决方法(0)

如何在Spring MVC 3.1中重定向后读取flash属性?

我想知道如何在Spring MVC 3.1中重定向后读取flash属性.

我有以下代码:

@Controller
@RequestMapping("/foo")
public class FooController {

  @RequestMapping(value = "/bar", method = RequestMethod.GET)
  public ModelAndView handleGet(...) {
    // I want to see my flash attributes here!
  }

  @RequestMapping(value = "/bar", method = RequestMethod.POST)
  public ModelAndView handlePost(RedirectAttributes redirectAttrs) {
    redirectAttrs.addFlashAttributes("some", "thing");
    return new ModelAndView().setViewName("redirect:/foo/bar");
  }

}
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

java spring-mvc

38
推荐指数
2
解决办法
5万
查看次数

为什么在GET重定向上清除SessionAttributes?

为简单起见,这些代码片段将缩短.这样做的目的是获取GET参数,在会话上设置它,并在删除url参数的情况下重定向回GET.基本上,URI清理.如果有更好/更简单的方法,我会很高兴听到它.

我有一个控制器定义如下:

@Controller
@RequestMapping("/path/page.xhtml")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@SessionAttributes({ "myParam1", "myParam2" })
public class MyController {

  @RequestMapping(method = RequestMethod.GET, params = { "urlParam2" })
  public String handleUriParam(@RequestParam(value = "urlParam2", required = false)
                               final Long urlParam2,
                               final RedirectAttributes redirs) {
    // at this point, myParam1 is set on the session.
    // now set the param as a flash attrib with the name of the session variable
    redirs.addFlashAttribute("myParam2", urlParam2);
    return "redirect:/path/page.xhtml";
  }

  @RequestMapping(method = RequestMethod.GET, params = {})
  public String doGetStuff(ModelMap model) {
    // do …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc

5
推荐指数
1
解决办法
4379
查看次数

标签 统计

java ×2

spring-mvc ×2

spring ×1