1. 程式人生 > >SpringMVC設定session

SpringMVC設定session



@Controller
@SessionAttributes("user") //放入使用者資訊  步驟1
public class UserController {
	@Resource
	UserService service;
	
	@RequestMapping("/logins")
	public String toLogin(){
		return "/jsp/login.jsp";
	}
	
	@RequestMapping(value = "/doLogin",method = RequestMethod.POST)
	public String doLogin(@RequestParam String username,@RequestParam String password,
			HttpServletRequest request,ModelMap map){
		//邏輯處理交給service
		try {
			User uer = service.doLogin(username, password);
			map.put("user", uer); //放到session中 步驟2
			//步驟3獲取sessionid
			String userInfo = request.getSession().getId();
			System.out.println("sseeion"+userInfo);
		} catch (Exception e) {
			request.setAttribute("error", e.getMessage());
			return "/jsp/login.jsp";
		}
	
		return "/jsp/main.jsp";
	}
	
	@RequestMapping("/dologout")
	public String doLogout(SessionStatus statues){
		//清除登入資訊
		statues.setComplete();
		
		return "/jsp/login.jsp";
	}
}