Spring MVC PRG模式,具有多个选项卡的会话解决方法



我有以下序列。

View1(POST表单)->PostController(创建模型并重定向)->GetController->View2

我正在使用RedirectAttributes在PostController和GetController之间传递模型,我有

类PostController{public字符串mypost(…,最终RedirectAttributes重定向属性){//创建模型redirectAttrs.addFlashAttribute("模型",model);return"redirect:myget";}}

@SessionAttributes("模型")类GetController{public ModelAndView myget(@ModelAttribute("model")final model model){ModelAndView mav=新ModelAndView("view2");mav.addObject("模型",model);返回mav;}}

当用户在浏览器上打开多个选项卡,然后刷新前一个选项卡时,它将被后一个打开的选项卡覆盖。

我希望每个标签都是独立的,希望有人能给我指明正确的方向。

谢谢。

编辑

问题出现在@SessionAttributes("模型")。我使用它是因为"Flash属性在重定向之前(通常在会话中)被临时保存,以便在重定向之后对请求可用,并且立即被删除。"。因此,由于会话中的模型被更新,选项卡会被相互覆盖。

通常,当我使用PRG时,我会尝试将所有相关属性放在重定向url中。像这样的。。。

public String myPost(ThingBean thingBean){
    Thing t = myService.updateThing(thingBean);
    return "redirect:thingView?id="+t.getId();    
}

这样,当您拦截重定向的get请求时,就不必依赖任何以前存储的会话数据。

@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";    
}

将模型保持为会话属性有点像将页面存储在全局变量中。这不是个好主意。当你点击页面刷新时,get请求只会发送url中的内容(如果你使用的话,可能还会发送一些cookie数据)。

最新更新