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