In Struts2, you sometimes need to directly use several http-type built-in objects, just like using built-in objects directly on JSP pages. There may be multiple activation methods, (the word "enable" is not enabled from off to off, because some built-in objects need to be encapsulated, packaged, or processed in Struts2, so the "enable" here is more about "finding out the original built-in objects". The image metaphor is a bit like exploring the essence of the content.) Below we will introduce three common methods.
1. IOC Injection
In struts2, how to enable the IOC method of http request, response, session, and application object is as follows:
1. Introduce the corresponding package to the Java File
[Java]
Import java. util. Map; // which objects are used to introduce the corresponding files,
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import javax. servlet. http. HttpSession;
Import org. apache. struts2.interceptor. ApplicationAware;
Import org. apache. struts2.interceptor. SessionAware;
Import org. apache. struts2.interceptor. ServletRequestAware;
Import org. apache. struts2.interceptor. ServletResponseAware;
Import org. apache. struts2.ServletActionContext; // struts2-related packages. import other packages as needed
Import com. opensymphony. xwork2.ActionContext;
Import com. opensymphony. xwork2.ActionSupport;
2. corresponding interfaces implemented in the Action class
Which of the following built-in objects can be used to implement the corresponding interfaces? For example:
[Java]
Public class RegisterAction extends ActionSupport
Implements HttpServletRequest, HttpServletResponse, SessionAware, ApplicationAware {
//... Other code...
}
3. Define the class variables request, response, session, and application within the class to implement the setter method in the corresponding interface.
[Java]
Private HttpServletRequest request; // HttpServletRequest variable request Declaration
Private HttpServletResponse response; // response declaration of the HttpServletResponse variable
Private Map session; // HttpSession variable session Declaration
Private Map application; // Application Variable application Declaration
Public void setServletRequest (HttpServletRequest request) {// method in the implementation Interface
This. request = request;
}
Public void setServletResponse (HttpServletResponse response) {// method in the implementation Interface
This. response = response;
}
Public void setSession (Map <String, Object> session) {// method in the implementation Interface
This. session = session;
}
Public void setApplication (Map <String, Object> application) {// method in the implementation Interface
This. application = application;
}
4. Use the request, response, session, and application objects.
After completing the preceding work, you can directly use these variables in execute () and other execution methods like in JSP files.
[Java]
Public String execute (){
//... Code...
String userName = request. getParameter ("userName"); // use
// Response. sendRedirect ("register/index. jsp ");
Session. put ("userName", userName );
Application. put ("onlineNum", 100 );
//... Code...
}
2. [recommendation]
In struts2, one of the other methods to enable request, response, session, and application objects is to use ActionContext to obtain the frontend input values. The steps are as follows:
1. Introduce the corresponding package to the Java File
[Java]
Import java. util. Map; // which objects are used to introduce the corresponding files?
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import javax. servlet. http. HttpSession;
Import org. apache. struts2.ServletActionContext; // struts2-related packages. import other packages as needed
Import com. opensymphony. xwork2.ActionContext;
Import com. opensymphony. xwork2.ActionSupport;
2. Obtain the Action Context
[Java]
ActionContext cxt = ActionContext. getContext ();
3. Get the corresponding object through the Context Environment and convert it to a variable of the corresponding type;
[Java]
HttpServletRequest request = (HttpServletRequest) cxt. get (ServletActionContext. HTTP_REQUEST );
HttpServletResponse response = (HttpServletResponse) cxt. get (ServletActionContext. HTTP_RESPONSE );
HttpSession session = (HttpSession) cxt. get (ServletActionContext. SESSION); // (HttpSession) cxt. getSession ();
Application application = (Application) cxt. get (ServletActionContext. APPLICATION); // (Application) cxt. getApplication ();
4. Use the request, response, session, and application objects.
After completing the preceding work, you can use these variables in execute () and other execution methods like in JSP files. The 4th Point class in the IOC method above applies;
3. Using ServletActionContext [recommended]
The entire struts2 is displayed as a Servlet in the web. xml file. Therefore, you can use the ServletActionContext class to obtain the context and the expected object.
1. Introduce the corresponding package to the Java File
[Java]
Import java. util. Map; // which objects are used to introduce the corresponding objects?
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import javax. servlet. http. HttpSession;
Import org. apache. struts2.ServletActionContext; // struts2-related packages. import other packages as needed
Import com. opensymphony. xwork2.ActionContext;
Import com. opensymphony. xwork2.ActionSupport;
2. Use the ServletActionContext class to obtain the Action Context Environment and then directly obtain the desired object.
[Java]
HttpServletRequest request = ServletActionContext. getRequest (); // directly obtain the request object
HttpServletResponse response = ServletActionContext. getResponse (); // directly obtain the response object
HttpSession session = (HttpSession) ServletActionContext. getContext (). getSession (); // get the session object
Map application = ServletActionContext. getContext (). getApplication (); // obtain the application object
// ActionContext cxt = ActionContext. getContext (); // obtain the context through ActionContext
ActionContext cxt = ServletActionContext. getContext ();
// Get the context through ServletActionContext. The result is the same as that of the previous line of code, but the retrieval method is different.
3. Use the request, response, session, and application objects.
After completing the preceding work, you can use these variables in execute () and other execution methods like in JSP files. The 4th Point class in the IOC method above applies;