SpringMVC框架在Controller中对于Session的处理
2016-05-15 16:21
519 查看
Controller中的代码:
如果用@SessionAttributes注解的话,博主暂时没有找出好方法可以单独删除session中的一个对象,即便是在action中传入参数HttpSession依然是只能获得session中的内容,无法删除,如果大家找到好办法能解决这个问题,还望能在评论中留下解决方法或者联系方式,博主将感激不尽!!
@Controller @SessionAttributes({"User","Y"}) //此处定义此Controller中将要创建和使用哪些session中的对象名 public class UserController { @Resource private UserService userService; @RequestMapping("user_login") public String login(User user, ModelMap modelMap){ //modelMap自动与session对应,你在往modelmap中添加对应属性便是往session中添加属性(前提是你已经在@SessionAttributes注解中定义好) User userTemp = this.userService.findByName(user.getName()); if((userTemp.getState() == 1) && (userTemp.getPassword().equals(user.getPassword())){ modelMap.addAttribute("User", userTemp); //成功将userTemp存入session中 modelMap.addAttribute("Y",1); //成功将1存入session中 return "/user/index"; }else { return "index"; } @RequestMapping("user_logout") public String logout(@ModelAttribute("User") User user, SessionStatus sessionStatus){ //@ModelAttribute("User")相当于将session中名为"User"的对象注入user对象中 //sessionStatus中的setComplete方法可以将session中的内容全部清空 sessionStatus.setComplete(); return "index"; } }User类的代码:
public class User { /** * This field was generated by MyBatis Generator. * This field corresponds to the database column user.id * * @mbggenerated */ private Integer id; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column user.name * * @mbggenerated */ private String name; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column user.password * * @mbggenerated */ private String password; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column user.email * * @mbggenerated */ private String email; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column user.activecode * * @mbggenerated */ private String activecode; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column user.state * * @mbggenerated */ private Integer state; /** * This method was generated by MyBatis Generator. * This method returns the value of the database column user.id * * @return the value of user.id * * @mbggenerated */ public Integer getId() { return id; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column user.id * * @param id the value for user.id * * @mbggenerated */ public void setId(Integer id) { this.id = id; } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column user.name * * @return the value of user.name * * @mbggenerated */ public String getName() { return name; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column user.name * * @param name the value for user.name * * @mbggenerated */ public void setName(String name) { this.name = name == null ? null : name.trim(); } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column user.password * * @return the value of user.password * * @mbggenerated */ public String getPassword() { return password; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column user.password * * @param password the value for user.password * * @mbggenerated */ public void setPassword(String password) { this.password = password == null ? null : password.trim(); } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column user.email * * @return the value of user.email * * @mbggenerated */ public String getEmail() { return email; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column user.email * * @param email the value for user.email * * @mbggenerated */ public void setEmail(String email) { this.email = email == null ? null : email.trim(); } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column user.activecode * * @return the value of user.activecode * * @mbggenerated */ public String getActivecode() { return activecode; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column user.activecode * * @param activecode the value for user.activecode * * @mbggenerated */ public void setActivecode(String activecode) { this.activecode = activecode == null ? null : activecode.trim(); } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column user.state * * @return the value of user.state * * @mbggenerated */ public Integer getState() { return state; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column user.state * * @param state the value for user.state * * @mbggenerated */ public void setState(Integer state) { this.state = state; }总结:
如果用@SessionAttributes注解的话,博主暂时没有找出好方法可以单独删除session中的一个对象,即便是在action中传入参数HttpSession依然是只能获得session中的内容,无法删除,如果大家找到好办法能解决这个问题,还望能在评论中留下解决方法或者联系方式,博主将感激不尽!!
相关文章推荐
- 基于Java的WebSocket推送
- java接口回调、同步回调、异步回调
- java 解析dom 树方式来解析XML文件
- leetcode 189
- java线程范围内的数据共享
- 客户端和服务端序列化操作的包名统一问题
- java mail发送邮件(单发和群发)
- Java面向对象之封装
- Java 中打印杨辉三角
- java中CAS方法分析
- Struts 2.0 拦截器原理解读
- 常用排序算法总结(Java)
- java在多网口的机器上的端口监听
- 【hibernate进阶】如何在myeclipse中添加jar包
- JAVA之旅(四)——面向对象思想,成员/局部变量,匿名对象,封装 , private,构造方法,构造代码块
- JAVA之旅(四)——面向对象思想,成员/局部变量,匿名对象,封装 , private,构造方法,构造代码块
- 希尔排序java版
- java transient关键字使用
- java 常用弹出框
- Java基础习题(二)——异常