gpt4 book ai didi

java - Spring MVC session 属性访问

转载 作者:IT老高 更新时间:2023-10-28 13:49:07 27 4
gpt4 key购买 nike

在 spring 3.0 下有什么方法可以访问 HttpSession 而不将其包含在方法签名中?我真正想做的是能够从可以为空的 HttpSession 中传递值。

类似这样的:

@RequestMapping("/myHomePage")
public ModelAndView show(UserSecurityContext ctx) {}

而不是这个:

@RequestMapping("/myHomePage")
public ModelAndView show(HttpSession session) {
UserSecurityContext ctx = (UserSecurityContext) session.getAttribute("userSecurityCtx");
}

最佳答案

@uthark 提到的 @SessionAttribute 注释不适合这个任务 - 我也认为是,但是 bit of reading否则显示:

Session attributes as indicated using this annotation correspond to a specific handler's model attributes, getting transparently stored in a conversational session. Those attributes will be removed once the handler indicates completion of its conversational session. Therefore, use this facility for such conversational attributes which are supposed to be stored in the session temporarily during the course of a specific handler's conversation.

For permanent session attributes, e.g. a user authentication object, use the traditional session.setAttribute method instead. Alternatively, consider using the attribute management capabilities of the generic WebRequest interface.

换句话说,@SessionAttribute 用于在 session 中存储 session MVC 模型对象(而不是将它们存储为请求属性)。它不适用于任意 session 属性。正如您所发现的,它仅在 session 属性始终存在时才有效。

我不知道任何其他选择,我认为你被 HttpSession.getAttribute()

所困扰

关于java - Spring MVC session 属性访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2213974/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com