Struts2 is decoupled from Servlet APIs, coupled access methods, struts2servlet

Source: Internet
Author: User

Struts2 is decoupled from Servlet APIs, coupled access methods, struts2servlet

1. Access Method decoupled from Servlet API

1. To avoid coupling with Servlet APIs and facilitate unit tests for Action classes,
Struts2 encapsulates HttpServletRequest, HttpSession, and ServletContext,
Three Map objects are constructed to replace these three objects. in Action,
Use the Map objects corresponding to HttpServletRequest, HttpSession, and ServletContext to save and read data.
To obtain the three Map objects, you can use the com. opensymphony. xwork2.ActionContext class.

ActionContext-request context. It is the request encapsulated by struts2. It contains the request, session, and application.The above three are Map.

Public class LoginAction implements Action {private String username; private String password; public String getUsername () {return username;} public void setUsername (String username) {this. username = username;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password;} public String execute () throws Exception {if (username. equals ("1") & password. equals ("1") {// decoupled record session Map <String, Object> session = ActionContext. getContext (). getSession (); session. put ("uname", username); return SUCCESS;} else {return LOGIN ;}}}
<! -- Logon

After successful login, record the user name to the Session. login fails. The jump will log on to the page

 -->        <action name="loginAction" class="cn.happy.action.LoginAction">            <result name="success">            s.jsp            </result>            <result name="login">            login.jsp            </result>        </action>

2. access method coupled with Servlet API

Directly accessing the Servlet API will make the Action class coupled with the Servlet API. The Servlet API objects are constructed by the Servlet container and bound with these objects, a Servlet container is required during the test, which is not convenient for testing the Action class. However, sometimes you do need to access these objects. Struts2 also provides a way to directly access the ServletAPI object.

To directly obtain the Servlet API object, you can use the org. apache. struts2.ServletActionContext class, which is a subclass of the ActionContext class.

2. Inject the Map object corresponding to the ServletAPI object to the Action.

Public class LoginAction implements Action, SessionAware {private String username; private String password; private Map <String, Object> map; public String execute () throws Exception {if (username. equals ("1") & password. equals ("1") {// inject map set. put ("uname", username); return SUCCESS;} else {return LOGIN ;}} public void setSession (Map <String, Object> map) {this. map = map ;}}

// Implemented through the ServletActionContext subclass of ActionContext

public class LoginAction implements Action {    private String username;    private String password;    public String execute() throws Exception {        if (username.equals("1")&&password.equals("1")) {           HttpSession session = ServletActionContext.getRequest().getSession();            session.setAttribute("uname", username);            return SUCCESS;        }else {            return LOGIN;        }    }

// Inject the Servlet API object to the Action instance

Public class LoginAction implements Action, ServletRequestAware {private HttpServletRequest Request; private String username; private String password; public String execute () throws Exception {if (username. equals ("1") & password. equals ("1") {// records session HttpSession session = request. getSession (); session. setAttribute ("uname", username); return SUCCESS;} else {return LOGIN;} public void setServletRequest (HttpServletRequest arg0) {this. request = request ;}

 

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.