【问题标题】:Session Handling in Spring MVC 3.0Spring MVC 3.0 中的会话处理
【发布时间】:2011-11-29 07:06:00
【问题描述】:

我在登录后使用session.setAttribute 来存储用户对象。在下一个控制器中,@SessionAttribute 用于同一用户,@ModelAttribute 用于同一对象,用于映射到RequestMapping 的方法中。登录后,如果我单击用户主页中的任何链接,它会给出

HttpSessionRequiredException: 会话属性 '' 必需 - 在会话中找不到

我不确定我做错了什么。我也在这个网站上浏览了许多文章和问题,但可以找到任何解决方案。我在会话中存储的用户对象存储用户的帐户详细信息,所有控制器都需要这些详细信息从数据库中获取不同的信息。我使用SessionAttribute 是错误的,我应该在所有控制器中使用HttpSession 并手动从会话中获取对象,或者在spring 3.0 中有适当的处理方法。请注意,此用户对象不支持任何形式的登录,而是包含许多其他详细信息。

因为帮助会很好。

【问题讨论】:

    标签: session spring-mvc


    【解决方案1】:

    看看我对会话数据的(非完美)使用:

    @Controller
    @SessionAttributes("sharedData")
    public class RegistrationFormController {
    
        @Autowired
        private SharedData sharedData; // bean with scope="session"
    
        @RequestMapping(value = {"/registrationForm"}, method = RequestMethod.GET)
        public ModelAndView newForm() {
            final ModelAndView modelAndView = new ModelAndView("registrationForm");
    
            modelAndView.addObject("registrationForm", new RegistrationForm());
            // I want to render some data from this object in JSP:
            modelAndView.addObject("sharedData", sharedData);
    
            return modelAndView;
        }
    
        @RequestMapping(value = {"/registrationForm"}, method = RequestMethod.POST)
        public String onRegistrationFormSubmitted(HttpServletRequest request,
                    @ModelAttribute("registrationForm") RegistrationForm registrationForm, BindingResult result) {
            if (result.hasErrors()) {
                return "registrationForm";
            }
    
            // Perform business logic, e.g. persist registration data
    
            return "formSubmitted";
        }
    }
    

    【讨论】:

    • 嗨 dma_k 我想这对我的情况没有帮助,因为我有用户详细信息要存储在会话中。在您的示例中,您存储 sharedData ,因为无法共享用户详细信息,因此我无法做到。
    • @Amit:您如何创建初始用户详细信息?例如。工厂方法UserDetials createUserDetails(...) {}签名怎么样?
    猜你喜欢
    • 1970-01-01
    • 2014-08-30
    • 2013-05-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 2020-12-16
    • 2012-04-30
    相关资源
    最近更新 更多