Spring MVC 重定向。模型和表格结算
Spring MVC Redirect. Model and Forms Clearing
假设我有一个 Web 表单,无论何时提交该表单,它都会经过验证。
如果出现错误,我会将其重定向到同一页面并使用 RedirectAttributes 来维护表单值以便重新提交。
否则,它成功提交到下一页。
问题:
(1) 每当我返回,或重新访问相同的表单 url,它总是保留提交的输入
(2) 尝试提交新值,它改变(更新)了之前提交的数据。
(3) 提交成功后希望清除为新的表单对象
到目前为止,这是我的控制器代码:
GET
@RequestMapping (method = RequestMethod.GET)
public String setForm(Model model, HttpSession session){
Object o;
if(!model.containsAttribute("object")){
model.addAttribute("object", new Object());
}
else{
l= (Object)modelasMap().get("object");
}
return "app/formUrl";
}
而在我的 POST
@RequestMapping(method = RequestMethod.POST)
public String formSubmit(@ModelAttribute("object" Object object,
final BindingResult result, HttpSession session,
SessionStatus status, RedirectAttributes attr){
//DO some object manipulation code here
//validation code
if(result.hasErrors()){
attr.addFlashAttribute(org.springframework.validation.BindingResult.object", result);
attr.addFlashAttribute("object", object);
return "redirect:/formUrl";
}
else{
//save form inputs here
}
}
return "redirect:successPage";
}
我真的很困惑为什么当我在我的 GET 中设置条件 if(!model.containsAttribute("object"))
时它没有返回一个新的清除表单。
我希望有人能向我解释似乎是什么问题。提前致谢!
编辑:
添加 <mvc:annotation-driven ignore-default-model-on-redirect = "true"/>
给我错误:
cvc-complex-type.3.2.2: Attribute 'ignoreDefaultModelOnRedirect' is not allowed to appear in element 'mvc:annotation-driven'.
如果我使用ignoreDefaultModelOnRedirect
同样的错误
附加信息 - 我正在构建 Maven。
您是否尝试过在您的 servlet 配置中将 ignore-default-model-on-redirect
属性设置为 true?如下:
<mvc:annotation-driven ignore-default-model-on-redirect="true" />
By default, the content of the "default" model is used both during rendering and redirect scenarios. Alternatively
a controller method can declare a RedirectAttributes argument and use it to provide attributes for a redirect.
Setting this flag to true ensures the "default" model is never used in a redirect scenario even if a
RedirectAttributes argument is not declared. Setting it to false means the "default" model may be used in a
redirect if the controller method doesn't declare a RedirectAttributes argument. The default setting is false but
new applications should consider setting it to true.
找到答案了。我用带有相应参数的 ModelAndView 替换了整个 GET 方法 (url, model, new Object()).
在 'if(result.hasErrors())' return 下应该是转发而不是重定向 url。
假设我有一个 Web 表单,无论何时提交该表单,它都会经过验证。
如果出现错误,我会将其重定向到同一页面并使用 RedirectAttributes 来维护表单值以便重新提交。 否则,它成功提交到下一页。
问题: (1) 每当我返回,或重新访问相同的表单 url,它总是保留提交的输入 (2) 尝试提交新值,它改变(更新)了之前提交的数据。 (3) 提交成功后希望清除为新的表单对象
到目前为止,这是我的控制器代码:
GET
@RequestMapping (method = RequestMethod.GET)
public String setForm(Model model, HttpSession session){
Object o;
if(!model.containsAttribute("object")){
model.addAttribute("object", new Object());
}
else{
l= (Object)modelasMap().get("object");
}
return "app/formUrl";
}
而在我的 POST
@RequestMapping(method = RequestMethod.POST)
public String formSubmit(@ModelAttribute("object" Object object,
final BindingResult result, HttpSession session,
SessionStatus status, RedirectAttributes attr){
//DO some object manipulation code here
//validation code
if(result.hasErrors()){
attr.addFlashAttribute(org.springframework.validation.BindingResult.object", result);
attr.addFlashAttribute("object", object);
return "redirect:/formUrl";
}
else{
//save form inputs here
}
}
return "redirect:successPage";
}
我真的很困惑为什么当我在我的 GET 中设置条件 if(!model.containsAttribute("object"))
时它没有返回一个新的清除表单。
我希望有人能向我解释似乎是什么问题。提前致谢!
编辑:
添加 <mvc:annotation-driven ignore-default-model-on-redirect = "true"/>
给我错误:
cvc-complex-type.3.2.2: Attribute 'ignoreDefaultModelOnRedirect' is not allowed to appear in element 'mvc:annotation-driven'.
如果我使用ignoreDefaultModelOnRedirect
附加信息 - 我正在构建 Maven。
您是否尝试过在您的 servlet 配置中将 ignore-default-model-on-redirect
属性设置为 true?如下:
<mvc:annotation-driven ignore-default-model-on-redirect="true" />
By default, the content of the "default" model is used both during rendering and redirect scenarios. Alternatively a controller method can declare a RedirectAttributes argument and use it to provide attributes for a redirect. Setting this flag to true ensures the "default" model is never used in a redirect scenario even if a RedirectAttributes argument is not declared. Setting it to false means the "default" model may be used in a redirect if the controller method doesn't declare a RedirectAttributes argument. The default setting is false but new applications should consider setting it to true.
找到答案了。我用带有相应参数的 ModelAndView 替换了整个 GET 方法 (url, model, new Object()).
在 'if(result.hasErrors())' return 下应该是转发而不是重定向 url。