简体   繁体   English

处理重叠的@SessionAttributes名称?

[英]Deal with overlapping @SessionAttributes names?

How can I make it so the @SessionAttributes are scoped to their respective controllers, or clean up the @SessionAttributes when switching workflows between controllers prematurely? 我如何才能做到@SessionAttributes的作用域@SessionAttributes于其各自的控制器,或者在过早切换控制器之间的工作流时清理@SessionAttributes

Example: 例:

User goes to webpage to edit a dictionary and comes into DictionaryController.java which creates a DictionaryForm.java object and stores it in @SessionAttributes under "form" 用户转到网页以编辑字典,然后进入DictionaryController.java ,后者创建一个DictionaryForm.java对象并将其存储在“表单”下的@SessionAttributes

Normally, the dictionary entry is fetched in a GET request, then updated on POST and status.setComplete() is called on success; 通常,在GET请求中获取字典条目,然后在POST上进行更新,并在成功时调用status.setComplete()

However if the user does the GET request, then clicks away to another page such as OrganizationController.java the second controller appears to try and reuse the "form" @SessionAttribute object from the other controller and will fail before it even reaches the getOrganization() method. 但是,如果用户发出GET请求,然后单击转到另一个页面(例如OrganizationController.java则第二个控制器似乎尝试从另一个控制器重用“ form” @SessionAttribute对象,并且在到达getOrganization()之前将失败方法。 (The nature of how exactly it's failing is undetermined as my eclipse console isn't outputting any exceptions, but I suspect it's because the form types don't match up) (由于我的eclipse控制台没有输出任何异常,所以不确定它到底有多准确,但我怀疑这是因为表单类型不匹配)

@SessionAttributes("form")
public class DictionaryController {

    @ModelAttribute("form")
    public DictionaryForm initForm() {
        return new DictionaryForm();
    }

    @RequestMapping(value="/Dictionary" method=RequestMethod.GET)
    public String getDictionary(
        @ModelAttribute("form") DictionaryForm form) {
        ...
        return "dictionaryView";
    }    

    @RequestMapping(value="/Dictionary" method=RequestMethod.POST)
    public String updateDictionary(
        @ModelAttribute("form") DictionaryForm form, 
        SessionStatus status) { 
        ...
        status.setComplete();
        return "successView";
    }

}
@Controller
@SessionAttributes("form")
public class OrganizationController{

    @ModelAttribute("form")
    public OrganizationForm initForm() {
        return new OrganizationForm();
    }

    @RequestMapping(value="/Organization" method=RequestMethod.GET)
    public String getOrganization(
        @ModelAttribute("form") OrganizationForm form) {
        ...
        return "orgView";
    }    

    @RequestMapping(value="/Organization" method=RequestMethod.POST)
    public String updateOrganization(
        @ModelAttribute("form") OrganizationForm form, 
        SessionStatus status) { 
        ...
        status.setComplete();
        return "successView";
    }
}

Solution I ended up using was having a BaseForm object type that all form types inherit from. 我最终使用的解决方案是具有所有表单类型都继承自的BaseForm对象类型。 Then in my request mapping methods for the GET requests, I would use @ModelAttribute("form") BaseForm form and manually check the form type in the body of the method, and if it doesn't match convert it and restore it in the session. 然后,在用于GET请求的请求映射方法中,我将使用@ModelAttribute("form") BaseForm form并手动检查该方法主体中的表单类型,如果不匹配,请将其转换并还原到会话。 (Replacing it in the session may be unnecessary if you attach it to your model object for the request) (如果将它附加到请求的模型对象上,则可能不需要在会话中替换它)

ie. 即。

@RequestMapping(value="/Organization" method=RequestMethod.GET)
    public String getOrganization(HttpServletRequest request,
        @ModelAttribute("form") BaseForm form) {
        if (form.getClass() != OrganizationForm.class) {
            form = new OrganizationForm();
            request.getSession().setAttribute("form", form);
        }
        ...
        return "orgView";
    }    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM