Java Spring 3.0 设置和获取会话属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2227395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring 3.0 set and get session attribute
提问by binary
I want to read a domain object (UserVO) from session scope.
我想从会话范围读取域对象 (UserVO)。
I am setting the UserVO in a controller called WelcomeController
我在名为 WelcomeController 的控制器中设置 UserVO
@Controller
@RequestMapping("/welcome.htm")
public class WelcomeController {
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(BindingResult result, SessionStatus status,HttpSession session){
User user = loginService.loginUser(loginCredentials);
session.setAttribute("user", user);
return "loginSuccess";
}
}
I am able to use the object in jsp pages <h1>${user.userDetails.firstName}</h1>
我可以在jsp页面中使用该对象 <h1>${user.userDetails.firstName}</h1>
But I am not able to read the value from another Controller,
但我无法从另一个控制器读取值,
I am trying to read the session attribute as follows:
我正在尝试按如下方式读取会话属性:
@Controller
public class InspectionTypeController {
@RequestMapping(value="/addInspectionType.htm", method = RequestMethod.POST )
public String addInspectionType(InspectionType inspectionType, HttpSession session)
{
User user = (User) session.getAttribute("user");
System.out.println("User: "+ user.getUserDetails().getFirstName);
}
}
采纳答案by skaffman
The code you've shown shouldwork - the HttpSession
is shared between the controllers, and you're using the same attribute name. Thus something else is going wrong that you're not showing us.
您显示的代码应该可以工作 - 在HttpSession
控制器之间共享,并且您使用相同的属性名称。因此,您没有向我们展示其他问题。
However, regardless of whether or not it works, Spring provides a more elegant approach to keeping your model objects in the session, using the @SessionAttribute
annotation (see docs).
但是,无论它是否有效,Spring 都提供了一种更优雅的方法来使用@SessionAttribute
注释将模型对象保留在会话中(请参阅文档)。
For example (I haven't tested this, but it gives you the idea):
例如(我没有测试过这个,但它给了你这个想法):
@Controller
@RequestMapping("/welcome.htm")
@SessionAttributes({"user"})
public class WelcomeController {
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(ModelMap modelMap){
User user = loginService.loginUser(loginCredentials);
modelMap.addtAttribute(user);
return "loginSuccess";
}
}
and then
进而
@Controller
@SessionAttributes({"user"})
public class InspectionTypeController {
@RequestMapping(value="/addInspectionType.htm", method = RequestMethod.POST )
public void addInspectionType(InspectionType inspectionType, @ModelAttribute User user) {
System.out.println("User: "+ user.getUserDetails().getFirstName);
}
}
However, if your original code isn't working, then this won't work either, since something else is wrong with your session.
但是,如果您的原始代码不起作用,那么这也不起作用,因为您的会话有其他问题。
回答by karpaczio
@SessionAttributes works only in context of particular handler, so attribute set in WelcomeController will be visible only in this controller.
@SessionAttributes 仅在特定处理程序的上下文中工作,因此在 WelcomeController 中设置的属性将仅在此控制器中可见。
回答by Shrirang Edgaonkar
Use a parent class to inherit all the controllers and use SessionAttributes over there. Just that this class should be in the package scan of mvc.
使用父类继承所有控制器并在那里使用 SessionAttributes。只是这个类应该在mvc的包扫描中。
回答by Anil Bhargava
May be you have not set your UserVO as Serializable.
可能是您没有将 UserVO 设置为可序列化。