Struts 2--struts 2 access to SERVLETAPI objects

Source: Internet
Author: User

Servletap parsing: API (Application Programming Interface) application programming interface.

Servletapi is a set of interfaces and classes related to Servlets.

To enable access to Servletapi objects, Struts 2 offers a number of ways, but it comes down to two broad categories:

1. How to access the SERVLETAPI decoupling

2. Access mode coupled to the SERVLETAPI

The decoupling method is described below:

1. Use the Actioncontex class to get the map object that corresponds to the Servletapi object.

 Public classLoginaction Implements Action {    PrivateUserInfo user =NewUserInfo (); @Override PublicString Execute () throws Exception {if(User.getusername (). Equals ("Admin") && User.getpassword (). Equals ("Admin")){             Map<string, object> session =Actioncontext.getcontext (). GetSession (); Session.put ("name", User.getusername ()); return "Success"; }Else{            return "Error"; }    }     PublicUserInfo GetUser () {returnuser; }     Public voidsetUser (UserInfo user) { This. user =user; }}

2. Inject the map object corresponding to the Servletapi object into the action

 Public classLoginaction implements Action,sessionaware {PrivateUserInfo user =NewUserInfo (); Private Map<string, object>map; @Override PublicString Execute () throws Exception {if(User.getusername (). Equals ("Admin") && User.getpassword (). Equals ("Admin") {map.put ("name", User.getusername ()); return "Success"; }Else{            return "Error"; }    }        //Sessionaware Implementation Method@Override Public voidSetsession (map<string, object>map) {         This. map=map; }         PublicUserInfo GetUser () {returnuser; }     Public voidsetUser (UserInfo user) { This. user =user; }     PublicMap<string, object>Getmap () {returnmap; }     Public voidSetmap (map<string, object>map) {         This. Map =map; }    }

The third type:

 Public classLoginaction implements Action,servletrequestaware {PrivateUserInfo user =NewUserInfo (); PrivateHttpServletRequest request; @Override PublicString Execute () throws Exception {if(User.getusername (). Equals ("Admin") && User.getpassword (). Equals ("Admin") {Request.setattribute ("name", User.getusername ()); return "Success"; }Else{            return "Error"; }    }        //Servletrequestaware Implementation Method@Override Public voidsetservletrequest (HttpServletRequest request) { This. request=request; }             Publichttpservletrequest getrequest () {returnrequest; }     Public voidsetrequest (HttpServletRequest request) { This. Request =request; }             PublicUserInfo GetUser () {returnuser; }     Public voidsetUser (UserInfo user) { This. user =user; }}

Coupling mode:

The first type:

 Public classLoginaction implements Action {PrivateUserInfo user =NewUserInfo (); @Override PublicString Execute () throws Exception {if(User.getusername (). Equals ("Admin") && User.getpassword (). Equals ("Admin") {HttpSession session=servletactioncontext.getrequest (). GetSession (); Session.setattribute ("name", User.getusername ()); return "Success"; }Else{            return "Error"; }    }     PublicUserInfo GetUser () {returnuser; }     Public voidsetUser (UserInfo user) { This. user =user; }  }

The second type:

 Public classLoginaction implements Action {PrivateUserInfo user =NewUserInfo (); @Override PublicString Execute () throws Exception {if(User.getusername (). Equals ("Admin") && User.getpassword (). Equals ("Admin") ) {Map<string, object> session =Servletactioncontext.getcontext (). GetSession (); Session.put ("name", User.getusername ()); return "Success"; }Else{            return "Error"; }    }     PublicUserInfo GetUser () {returnuser; }     Public voidsetUser (UserInfo user) { This. user =user; }    }

Struts 2--struts 2 access to SERVLETAPI objects

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.