SPRINGMVC two ways to access the session

Source: Internet
Author: User

Method One: Use Servlet-api

[Java]View PlainCopyprint?
  1. @Controller
  2. Public class Managercontroller {
  3. @Resource
  4. private ManagerService Managerserviceimpl;
  5. @RequestMapping (value = "Manager/login.do", method = Requestmethod.get)
  6. Public Modelandview Login (Managermodel managermodel,httpsession HttpSession) {
  7. Managermodel manager = Managerserviceimpl.getmanager (Managermodel);
  8. if (manager!=null) {
  9. Manager.setpassword ("");
  10. Httpsession.setattribute ("manager", manager);
  11. return New Modelandview (new Redirectview (".  /admin/main.jsp "));
  12. }else{
  13. return New Modelandview (new Redirectview (".  /admin/login.jsp "));
  14. }
  15. }
  16. @RequestMapping (value = "Manager/logout.do", method = Requestmethod.get)
  17. Public String Logout (HttpSession HttpSession) {
  18. Httpsession.getattribute ("manager");
  19. return "Success";
  20. }
  21. }



Method Two: Use Sessionattributes

[Java]View PlainCopyprint?
    1. @Controller
    2. @SessionAttributes ("manager")
    3. Public class Managercontroller {
    4. @Resource
    5. private ManagerService Managerserviceimpl;
    6. @RequestMapping (value = "Manager/login.do", method = Requestmethod.get)
    7. Public Modelandview Login (Managermodel managermodel,modelmap model) {
    8. Managermodel manager = Managerserviceimpl.getmanager (Managermodel);
    9. if (manager!=null) {
    10. Manager.setpassword ("");
    11. Model.addattribute ("manager", manager);
    12. return New Modelandview (new Redirectview (".  /admin/main.jsp "));
    13. }else{
    14. return New Modelandview (new Redirectview (".  /admin/login.jsp "));
    15. }
    16. }
    17. @RequestMapping (value = "Manager/logout.do", method = Requestmethod.get)
    18. Public String Logout (@ModelAttribute ("manager") Managermodel Managermodel) {
    19. return "Success";
    20. }
    21. }

SPRINGMVC two ways to access the session

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.