Spring MVC PRG pattern with multiple tabs session workaround
I have the following sequence.
View1 (POST form) -> PostController (create model and redirect) -> GetController -> View2
I am using RedirectAttributes to pass model between PostController and GetController, I have
class PostController { public String mypost(..., final RedirectAttributes redirectAttrs){ //create model redirectAttrs.addFlashAttribute("model", model); return "redirect:myget"; } }
and
@SessionAttributes("model") class GetController { public ModelAndView myget(@ModelAttribute("model") final Model model){ ModelAndView mav = new ModelAndView("view2"); mav.addObject("model", model); return mav; } }
When a user opens multiple tabs on a browser, then refresh the earlier tab, it will be overwritten by the latter opened tab.
I would like each tab to be independent, hope someone point me to the right direction.
Thanks.
Edit
The problem is at @SessionAttributes("model"). I use it because "Flash attributes are saved temporarily before the redirect (typically in the session) to be made available to the request after the redirect and removed immediately.". Thus, tabs are overwritten each other because the model in session is updated.
Typically when I use PRG I try to put all the relevant attributes in the redirect url. Something like this...
public String myPost(ThingBean thingBean){
Thing t = myService.updateThing(thingBean);
return "redirect:thingView?id="+t.getId();
}
That way when you intercept the redirected get request you don't have to rely on any previously stored session data.
@RequestMapping(value="thingView",method=RequestMethod.Get)
public String thingView(Map<String,Object> model, @RequestParam(value="id") Integer id){
model.put("thing",myService.getThing(id));
return "thing/viewTemplate";
}
Keeping your model as a session attribute is kind of like storing your page in a global variable. It's not a good idea. And when you hit refresh on a page the get request is only going to send what's in the url (and maybe some cookie data if you're using that).
链接地址: http://www.djcxy.com/p/68078.html上一篇: Spring获取属性方法时出错