2011-11-28 70 views
1

我使用session.setAttribute來登錄後存儲用戶對象。在下一個控制器中,對於同一用戶,我有@SessionAttribute,對於映射到RequestMapping的方法中使用的同一對象,我有@ModelAttribute。如果我點擊在用戶主頁的任何鏈接登錄後,它給Spring MVC 3.0中的會話處理

HttpSessionRequiredException:會話屬性「」必需的 - 會議

沒有發現我不知道我做錯了。我在這個網站上瀏覽了很多文章和問題,但可以找到任何解決方案。我在會話中存儲的用戶對象存儲用戶的帳戶詳細信息,這是所有控制器中要求從DB獲取不同信息的帳戶詳細信息。如果我在所有控制器中使用HttpSession,並使用SessionAttribute錯誤,並且手動從會話中獲取對象,或者在3.0版中有合適的方法來處理。請注意,此用戶對象不支持任何形式,只是登錄,但包含許多其他細節。

因爲幫助會很好。

回答

0

看一看我的(非完美)使用會話的數據:

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

嗨dma_k我想這不會有幫助我的情況,因爲我有用戶詳細信息存儲在會話。在你的例子中,我存儲sharedData,我不能做,因爲用戶的細節不能共享。 – Amit

+0

@Amit:你如何創建你的初始用戶細節?例如。工廠方法'UserDetials createUserDetails(...){}'簽名是怎麼樣的? –