spring 会话错误 [无法公开会话属性“用户”;因为存在同名的模型对象]
您好,我收到以下错误
javax.servlet.ServletException: Cannot expose session attribute 'user' because of an existing model object of the same name
org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:141)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
代码
@Controller
@RequestMapping("/admin")
@SessionAttributes("user")
public class AdminHome {
@RequestMapping("home")
public String homePage(HttpSession session, ModelMap map) {
map.addAttribute(org.brahmaa.ads.util.impl.UserInfo.getPrincipal(session));
return "admin/home";
}
}
Hi i am getting following error
javax.servlet.ServletException: Cannot expose session attribute 'user' because of an existing model object of the same name
org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:141)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
Code
@Controller
@RequestMapping("/admin")
@SessionAttributes("user")
public class AdminHome {
@RequestMapping("home")
public String homePage(HttpSession session, ModelMap map) {
map.addAttribute(org.brahmaa.ads.util.impl.UserInfo.getPrincipal(session));
return "admin/home";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在 spring-boot 中使用这个属性
I use property this in spring-boot
据我了解,
map.addAttribute(org.brahmaa.ads.util.impl.UserInfo.getPrincipal(session));
从会话中提取user
属性并将其放入模型中。@SessionAttriubtes
指示 Spring 自动执行相同的操作。因此他们发生冲突。您应该只使用这些方法中的一种,而不是同时使用两种方法。As far as I understand,
map.addAttribute(org.brahmaa.ads.util.impl.UserInfo.getPrincipal(session));
extracts theuser
attribute from the session and puts it into model.@SessionAttriubtes
instructs Spring to do the same automatically. Therefore they conflicts. You should use only one of these approaches, not both simultaneously.我也突然遇到了这个异常。结果我添加了这个 Freemarker 属性:
I suddenly got this exception as well. Turns out I had added this Freemarker property: