欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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"; //給出其他提示信息