Method One: Use Servlet-api
@Controller Public classManagercontroller {@ResourcePrivateManagerService Managerserviceimpl; @RequestMapping (Value="manager/login.do", method =requestmethod.get) PublicModelandview Login (Managermodel managermodel,httpsession HttpSession) {Managermodel Manager=Managerserviceimpl.getmanager (Managermodel); if(manager!=NULL) {Manager.setpassword (""); Httpsession.setattribute ("Manager", manager); return NewModelandview (NewRedirectview (".. /admin/main.jsp")); }Else{ return NewModelandview (NewRedirectview (".. /admin/login.jsp")); }} @RequestMapping (Value="manager/logout.do", method =requestmethod.get) PublicString Logout (HttpSession HttpSession) {Httpsession.getattribute ("Manager"); return "Success"; }}
Method Two: Use Sessionattributes
@Controller @sessionattributes ("Manager") Public classManagercontroller {@ResourcePrivateManagerService Managerserviceimpl; @RequestMapping (Value="manager/login.do", method =requestmethod.get) PublicModelandview Login (Managermodel managermodel,modelmap model) {Managermodel Manager=Managerserviceimpl.getmanager (Managermodel); if(manager!=NULL) {Manager.setpassword (""); Model.addattribute ("Manager", manager); return NewModelandview (NewRedirectview (".. /admin/main.jsp")); }Else{ return NewModelandview (NewRedirectview (".. /admin/login.jsp")); }} @RequestMapping (Value="manager/logout.do", method =requestmethod.get) PublicString Logout (@ModelAttribute ("Manager") Managermodel Managermodel) {return "Success"; }}
Two methods of SPRINGMVC access session