我有一个类:
@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;
}
}
我的问题:此代码导致在 session 中存储“结果”对象,但我想在 session 中存储“result.getSessionId()”。
我无法将“模型模型”添加到“loginStatic”方法的输入参数中,因为它会更改方法签名,而我现在无法执行此操作。 而且我也无法明确获取 http session 并在其中设置属性(由于其他副作用)。 我怎么能那样做呢? 谢谢..
最佳答案
您可以使用<mvc:interceptors/>
注册定制 HandlerInterceptor
这将适用于一个、多个或所有 Controller 。例如,以下是如何为所有 Controller 注册拦截器:
<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 版本。
关于java - 如何在 spring mvc 中没有模型对象的情况下向 session 添加属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14230276/