spring mvc3中的addFlashAttribute方法
程序员文章站
2022-07-15 08:57:23
...
记得在spring mvc2中,当保存POJO到数据库后,要返回成功页面,如果这个时候要带点信息,
则要这样:
Java代码
1. //第三个参数(UserModel user)默认为绑定对象
2. @RequestMapping(value = "/user/save", method = RequestMethod.POST)
3. public ModelAndView saveUser(HttpServletRequest request, HttpServletResponse response,UserModel user) throws Exception {
4. ModelAndView mv = new ModelAndView("/user/save/result");//默认为forward模式
5.// ModelAndView mv = new ModelAndView("redirect:/user/save/result");//redirect模式
6. mv.addObject("message","保存用户成功!");
7. return mv;
8. }
//第三个参数(UserModel user)默认为绑定对象
@RequestMapping(value = "/user/save", method = RequestMethod.POST)
public ModelAndView saveUser(HttpServletRequest request, HttpServletResponse response,UserModel user) throws Exception {
ModelAndView mv = new ModelAndView("/user/save/result");//默认为forward模式
// ModelAndView mv = new ModelAndView("redirect:/user/save/result");//redirect模式
mv.addObject("message","保存用户成功!");
return mv;
}
而在spring mvc 3.1后,可以这样
Java代码
1.@RequestMapping(value = "/user/save", method = RequestMethod.POST)
2.public ModelAndView saveUser(UserModel user, RedirectAttributes redirectAttributes) throws Exception {
3. redirectAttributes.addFlashAttribute("message", "保存用户成功!");//使用addFlashAttribute,参数不会出现在url地址栏中
4. return "redirect:/user/save/result";
5.}
@RequestMapping(value = "/user/save", method = RequestMethod.POST)
public ModelAndView saveUser(UserModel user, RedirectAttributes redirectAttributes) throws Exception {
redirectAttributes.addFlashAttribute("message", "保存用户成功!");//使用addFlashAttribute,参数不会出现在url地址栏中
return "redirect:/user/save/result";
}
来个稍微完整点的例子,首先是一个表单,在其中填入一些信息:
Java代码
1.<form:form id="myform" action="saveUserDetails.action" method="POST" commandName="user">
2. <form:input type="text" name="firstName" path="firstName"/>
3. <form:input type="text" name="lastName" path="lastName"/>
4. <form:input type="text" name="email" path="email"/>
5. <input type="submit" value="submit">
6.</form:form>
7.
<form:form id="myform" action="saveUserDetails.action" method="POST" commandName="user">
<form:input type="text" name="firstName" path="firstName"/>
<form:input type="text" name="lastName" path="lastName"/>
<form:input type="text" name="email" path="email"/>
<input type="submit" value="submit">
</form:form>
则在controller中,可以这样:
Java代码
1.
2. @RequestMapping(value="/saveUserDetails.action", method=RequestMethod.POST)
3.public String greetingsAction(@Validated User user,RedirectAttributes redirectAttributes){
4.
5. someUserdetailsService.save(user);
6.
7. redirectAttributes.addFlashAttribute("firstName", user.getFirstName());
8. redirectAttributes.addFlashAttribute("lastName", user.getLastName())
9. return "redirect:success.html";
10.}
@RequestMapping(value="/saveUserDetails.action", method=RequestMethod.POST)
public String greetingsAction(@Validated User user,RedirectAttributes redirectAttributes){
someUserdetailsService.save(user);
redirectAttributes.addFlashAttribute("firstName", user.getFirstName());
redirectAttributes.addFlashAttribute("lastName", user.getLastName())
return "redirect:success.html";
}
success.html:
<div>
<h1>Hello ${firstName} ${lastName}. Your details stored in our database.</h1>
</div><br>
但如果F5的时候,会发现参数丢失,因为flash scope其实只支持redirect的,所以可以判断下:
Java代码
1.@RequestMapping(value="/success.html", method=RequestMethod.GET)
2. public String successView(HttpServletRequest request){
3. Map<String,?> map = RequestContextUtils.getInputFlashMap(request);
4. if (map!=null)
5. return "success";
6. else return "redirect:someOtherView"; //給出其他提示信息
则要这样:
Java代码
1. //第三个参数(UserModel user)默认为绑定对象
2. @RequestMapping(value = "/user/save", method = RequestMethod.POST)
3. public ModelAndView saveUser(HttpServletRequest request, HttpServletResponse response,UserModel user) throws Exception {
4. ModelAndView mv = new ModelAndView("/user/save/result");//默认为forward模式
5.// ModelAndView mv = new ModelAndView("redirect:/user/save/result");//redirect模式
6. mv.addObject("message","保存用户成功!");
7. return mv;
8. }
//第三个参数(UserModel user)默认为绑定对象
@RequestMapping(value = "/user/save", method = RequestMethod.POST)
public ModelAndView saveUser(HttpServletRequest request, HttpServletResponse response,UserModel user) throws Exception {
ModelAndView mv = new ModelAndView("/user/save/result");//默认为forward模式
// ModelAndView mv = new ModelAndView("redirect:/user/save/result");//redirect模式
mv.addObject("message","保存用户成功!");
return mv;
}
而在spring mvc 3.1后,可以这样
Java代码
1.@RequestMapping(value = "/user/save", method = RequestMethod.POST)
2.public ModelAndView saveUser(UserModel user, RedirectAttributes redirectAttributes) throws Exception {
3. redirectAttributes.addFlashAttribute("message", "保存用户成功!");//使用addFlashAttribute,参数不会出现在url地址栏中
4. return "redirect:/user/save/result";
5.}
@RequestMapping(value = "/user/save", method = RequestMethod.POST)
public ModelAndView saveUser(UserModel user, RedirectAttributes redirectAttributes) throws Exception {
redirectAttributes.addFlashAttribute("message", "保存用户成功!");//使用addFlashAttribute,参数不会出现在url地址栏中
return "redirect:/user/save/result";
}
来个稍微完整点的例子,首先是一个表单,在其中填入一些信息:
Java代码
1.<form:form id="myform" action="saveUserDetails.action" method="POST" commandName="user">
2. <form:input type="text" name="firstName" path="firstName"/>
3. <form:input type="text" name="lastName" path="lastName"/>
4. <form:input type="text" name="email" path="email"/>
5. <input type="submit" value="submit">
6.</form:form>
7.
<form:form id="myform" action="saveUserDetails.action" method="POST" commandName="user">
<form:input type="text" name="firstName" path="firstName"/>
<form:input type="text" name="lastName" path="lastName"/>
<form:input type="text" name="email" path="email"/>
<input type="submit" value="submit">
</form:form>
则在controller中,可以这样:
Java代码
1.
2. @RequestMapping(value="/saveUserDetails.action", method=RequestMethod.POST)
3.public String greetingsAction(@Validated User user,RedirectAttributes redirectAttributes){
4.
5. someUserdetailsService.save(user);
6.
7. redirectAttributes.addFlashAttribute("firstName", user.getFirstName());
8. redirectAttributes.addFlashAttribute("lastName", user.getLastName())
9. return "redirect:success.html";
10.}
@RequestMapping(value="/saveUserDetails.action", method=RequestMethod.POST)
public String greetingsAction(@Validated User user,RedirectAttributes redirectAttributes){
someUserdetailsService.save(user);
redirectAttributes.addFlashAttribute("firstName", user.getFirstName());
redirectAttributes.addFlashAttribute("lastName", user.getLastName())
return "redirect:success.html";
}
success.html:
<div>
<h1>Hello ${firstName} ${lastName}. Your details stored in our database.</h1>
</div><br>
但如果F5的时候,会发现参数丢失,因为flash scope其实只支持redirect的,所以可以判断下:
Java代码
1.@RequestMapping(value="/success.html", method=RequestMethod.GET)
2. public String successView(HttpServletRequest request){
3. Map<String,?> map = RequestContextUtils.getInputFlashMap(request);
4. if (map!=null)
5. return "success";
6. else return "redirect:someOtherView"; //給出其他提示信息
上一篇: js常用的验证
下一篇: 一起来讨论程序员的跳槽与选择
推荐阅读
-
Android中系统默认输入法设置的方法(输入法的显示和隐藏)
-
asp.net中System.Timers.Timer的使用方法_javascript技巧
-
在iframe中隐藏横向滚动条的方法大全
-
Android string.xml中的替换方法
-
Android中EditText光标在4.0中的bug及解决方法
-
把spring boot项目发布tomcat容器(包含发布到tomcat6的方法)
-
Android中传递对象的三种方法的实现
-
ASP.NET MVC中图表控件的使用方法
-
使用 Spring Boot 内嵌容器 Undertow创建服务器的方法
-
spring中aop的xml配置方法实例详解