Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring MVC 3.1中,如何在重定向后读取flash属性?_Java_Spring Mvc - Fatal编程技术网

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

Java 在Spring MVC 3.1中,如何在重定向后读取flash属性?,java,spring-mvc,Java,Spring Mvc,我想知道在SpringMVC3.1中重定向后如何读取flash属性 我有以下代码: @Controller @RequestMapping("/foo") public class FooController { @RequestMapping(value = "/bar", method = RequestMethod.GET) public ModelAndView handleGet(...) { // I want to see my flash attributes

我想知道在SpringMVC3.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");
  }

}

我缺少什么?

使用
模型
,它应该预先填充flash属性:

@RequestMapping(value = "/bar", method = RequestMethod.GET)
public ModelAndView handleGet(Model model) {
  String some = (String) model.asMap().get("some");
  // do the job
}
或者,您也可以使用:

我在代码中使用了
RedirectAttributes
model.asMap()
方法,它工作正常。

尝试以下操作:

@Controller
public class FooController
{
    @RequestMapping(value = "/foo")
    public String handleFoo(RedirectAttributes redirectAttrs)
    {
        redirectAttrs.addFlashAttribute("some", "thing");
        return "redirect:/bar";
    }

    @RequestMapping(value = "/bar")
    public void handleBar(@ModelAttribute("some") String some)
    {
        System.out.println("some=" + some);
    }
}

Spring MVC 3.2.2适用于所有像我这样的人,他们在验证失败时在浏览器中查看帖子url时遇到问题

POST url是一个私有url,不应向用户公开,但在验证失败时自动呈现。i、 e.如果字段低于最小长度。我用的是@Valid。我希望表单的原始GET url始终显示,即使验证返回到表单时也是如此,因此我执行了以下操作:

        if (validation.hasErrors()) {

        redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.story", validation);
        redirectAttributes.addFlashAttribute("story", story);
        return new ModelAndView("redirect:/january/2015");
其中story是表单对象表示,redirectAttributes是您放入方法签名中的redirectAttributes,validation是BindingResult/2015年1月是表单所在的GET控制器的映射

实施后,在2015年1月/1月的映射中,故事完整无缺,如下所示:

Story story= (Story) model.asMap().get("story");
//story from the POST method
我必须扩充我的GET方法并检查它是否为null。如果不为null,则将其发送到表单,否则我会将新初始化的故事类型作为默认行为发送到表单


通过这种方式,我可以在bindingresults保持不变的情况下返回表单(表单上显示错误),但我的GET url代替了post url。

谢谢,我会尝试一下。运气不好,我的模型是空的。您是否尝试了
RequestContextUtils.getInputFlashMap(请求)
?我将尝试返回
重定向视图
,然后再返回
字符串
。谢谢,返回
重定向视图
,效果非常好。请注意,您无法检查自定义对象上的“属性是否已填充”。
@modeldattribute
即使重定向属性中没有具有给定名称的对象,也返回NOTNULL。
        if (validation.hasErrors()) {

        redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.story", validation);
        redirectAttributes.addFlashAttribute("story", story);
        return new ModelAndView("redirect:/january/2015");
Story story= (Story) model.asMap().get("story");
//story from the POST method