问题描述
如在登录时设置@SessionAttributes("user")对象,在其它Controller的方法里要通过@ModelAttribute("user")取该对象,同时参数里有表单对象:public Map<String,Object> save(@ModelAttribute("user")User user, Orders form){...}表单里有和User对象相同的属性,其值会被覆盖,为啥InternalResourceViewResolver没有allowSessionOverride属性设置不许覆盖,而FreeMarkerViewResolver却有?
解决方案
1、freemarker等非JSP模板没有request、session等概念,只有模型的概念,按你说的allowSessionOverride表示是否允许session属性覆盖模型数据,默认是false,即不允许,如果重名则抛异常2、EL表达式如${a.id} 默认先从pageContext--->request--->session--->application/** * Set whether HttpSession attributes are allowed to override (hide) * controller generated model attributes of the same name. Default is "false", * which causes an exception to be thrown if session attributes of the same * name as model attributes are found. */public void setAllowSessionOverride(boolean allowSessionOverride) {this.allowSessionOverride = allowSessionOverride;}3、如在登录时设置@SessionAttributes("user")对象,在其它Controller的方法里要通过@ModelAttribute("user")取该对象,同时参数里有表单对象:建议session里的登录用户使用如loginUser 等不同的标识符来存储,和普通的区分开