[英]how add attribute to session without having model object in spring mvc?
我有一個像這樣的課程:
@Controller(value = "services")
@RequestMapping("/")
@SessionAttributes({"SESSIONID"})
public class Services {
@RequestMapping("/user/loginStatic")
@ModelAttribute("SESSIONID")
public LoginResponseBean loginStatic(String username){
LoginResponseBean result = otherClass.login(username);
retrun result;
}
}
我的問題:此代碼是導致在會話中存儲“結果”對象的原因,但我想在會話中存儲“ result.getSessionId()”。
我無法將“ Model model”添加到“ loginStatic”方法的輸入參數中,因為它會更改方法的簽名,我現在無法執行。 而且我也不能顯式地獲取http會話並在其中設置屬性(由於其他一些副作用)。 我該怎么辦? 謝謝..
您可以使用<mvc:interceptors/>
注冊一個適用於一個,多個或所有控制器的自定義HandlerInterceptor
。 例如,以下是為所有控制器注冊攔截器的方法:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="a.b.c.MyHandlerInterceptorAdapter"/>
</mvc:interceptor>
</mvc:interceptors>
另請參閱mvc-config-interceptors
和mvc-handlermapping-interceptor
文檔以獲取更多詳細信息。
注意:Spring 3.2文檔已鏈接,因此您可能需要更改URL中的發行版號以匹配您所使用的Spring版本。
在這里看看已接受的答案: 如何在春季獲得會話對象
您可以如此處所述獲取會話對象並添加屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.