// 登陆请求方法 @RequestMapping(value = "/login", method = RequestMethod.POST) public String login(TbUser user, String ishave, HttpServletRequest request,HttpSession session, HttpServletResponse response) { if ("remeber".equals(ishave)) { // 记住账号 // 如果勾选了机主账户,则需要把用户名和勾选状态保存到Cookie! Cookie stateCookie = new Cookie("remeber", "checked='checked'"); Cookie nameCookie = new Cookie("loginName", user.getLoginname()); // 设置Cookie的过期时间 stateCookie.setMaxAge(Integer.MAX_VALUE); nameCookie.setMaxAge(Integer.MAX_VALUE); // 保存Cookie response.addCookie(stateCookie); response.addCookie(nameCookie); } else { // 没有勾选,则清除Cookie信息. Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie ck : cookies) { ck.setMaxAge(0); response.addCookie(ck); } } } // 执行登陆操作----查询user表 TbUser login = userService.login(user); if (login != null) { session.setAttribute("user_session", login); session.setMaxInactiveInterval(24 * 60 * 60); return "index"; } return "login"; }
在如上的controller将取到的user放入user_session这个session中了,在index页面中取值
<strong class="font-bold">${sessionScope.user_session.username}</strong>
能取出,但是在我的home.jsp中无法取值
页面如图页面代码
<h2 class="font-bold">欢迎你,${sessionScope.user_session.username}</h2>