Struts2 series notes (4) --- Ation class access servlet, struts2 --- ation
Ation access servlet
There are three methods for the Ation class servlet:
(1) Access Servlet API in an indirect way --- use ActionContext object
(2) Implement the interface and complete the injection when accessing the Action
(3) obtain the Servlet API directly through ServletActionContext
The first one is described below:
(1) Access Servlet API in an indirect way --- use ActionContext object
Struts2 provides an ActionContext class (the context object of the current Action), which can be used to access the Servlet API. Below are several common methods provided in this class:
1. public static ActionContext getContext (): obtains the ActionContext instance of the current Action. |
2. public Object get (Object key): This method is similar to calling the getAttribute (String name) method of HttpServletRequest. |
3. public void put (Object key, Object value): This method is similar to calling setAttribute (String name, Object o) of HttpServletRequest ). |
4. public Map getParameters (): Get all request parameters. Similar to calling the getParameterMap () method of the HttpServletRequest object. |
5. public Map getSession (): returns a Map object that simulates an HttpSession instance. |
6. public void setSession (Map session): directly input a Map instance and convert the key-value pair in the Map instance to the attribute name-attribute value pair of the session. |
7. public Map getApplication (): returns a Map object that simulates the ServletContext instance of the application. |
8. public void setApplication (Map application): directly input a Map instance and The key-value pair of is converted to the attribute name-attribute value pair of the application. |
1 import java. util. map 2 import com. opensymphony. xwork2.ActionContext; 3 import com. opensymphony. xwork2.ActionSupport; 4 public class LoginAction1 extends ActionSupport {5 6 public String login () throws Exception {7 // get ActionContext 8 ActionContext actionContext = ActionContext. getContext (); 9 // 1. obtain the form's request parameter 10 Map <String, Object> parameters = actionContext. getParameters (); 11 // because there may be multiple usernames, array 12 String [] values = (String []) parameters is returned. get ("username"); 13 System. out. println ("username:" + values [0]); 14 15 // 2. access the Attribute16 actionContext of the request. put ("company", "Rain point name"); // It is equivalent to request. setAttribute ("", ""); 17 System. out. println (actionContext. get ("company"); 18 19 // 3. access the Attribute20 Map of the session <String, Object> sessionMap = actionContext. getSession (); 21 sessionMap. put ("age", 11); // equivalent to session. setAttribute ("", ""); 22 System. out. println (sessionMap. get ("age"); 23 24 // 4. access Attribute25 Map of application <String, Object> applicationMap = actionContext. getApplication (); 26 applicationMap. put ("info", "second stage of next week's test"); 27 System. out. println (applicationMap. get ("info"); 28 return SUCCESS; 29} 30}
(2) Implement the interface and complete the injection when accessing the Action
For structure injection, three interfaces are required.
(1) ServletContextAware: The Action that implements this interface can directly access the ServletContext instance of the Web application; |
(2) ServletRequestAware: The Action that implements this interface can directly access the HttpServletRequest instance of the Web application; |
(3) ServletResponseAware: The Action that implements this interface can directly access the HttpServletResponset instance of the Web application. |
1 import java. util. map; 2 3 import javax. servlet. servletContext; 4 import javax. servlet. servletResponse; 5 import javax. servlet. http. httpServletRequest; 6 import javax. servlet. http. httpServletResponse; 7 import org. apache. struts2.interceptor. requestAware; 8 import org. apache. struts2.interceptor. servletRequestAware; 9 import org. apache. struts2.interceptor. servletResponseAware; 10 import org. apache. struts2.util. servletContextAware; 11 12 import com. opensymphony. xwork2.ActionContext; 13 import com. opensymphony. xwork2.ActionSupport; 14 15 public class LoginAction2 extends ActionSupport implements packages, 16 ServletContextAware, ServletResponseAware {17 18 private HttpServletRequest request; 19 20 private ServletContext context; 21 22 private HttpServletResponse response; 23 24 public String login () throws Exception {25 // 1. obtain the form's request parameter 26 System. out. println (request. getParameter ("username"); 27 28 // 2. attribute29 request for access request. setAttribute ("company", "hzgg"); 30 31 // 3. the Attribute32 request to access the session. getSession (). setAttribute ("age", 50); 33 34 // 4. access Attribute35 context of the application. setAttribute ("info", "you can go out to play this afternoon"); 36 37 return SUCCESS; 38 39} 40 // Method 41 public void setServletRequest (HttpServletRequest request) required to implement the ServletRequestAware interface {42 this. request = request; 43} 44 // method required to implement the ServletContextAware interface 45 public void setServletContext (ServletContext context) {46 this. context = context; 47} 48 // method 49 public void setServletResponse (HttpServletResponse response) required to implement the ServletResponseAware interface {50 this. response = response; 51} 52 53}
(3) Access the Servlet API using the ServletActionContext tool class
Several common methods (static methods) in the ServletActionContext tool class ):
(1) PageContext getPageContext (): obtains the PageContext object of the application; |
(2) HttpServletRequest getRequest (): obtains the HttpServletRequest object of the application; |
(3) HttpServletRequest getResponse (): obtains the HttpServletResponse object of the application; |
(4) ServletContext getServletContext (): gets the ServletContext object of the application. |
1 import org. apache. struts2.ServletActionContext; 2 import com. opensymphony. xwork2.ActionSupport; 3 4 public class LoginAction3 extends ActionSupport {5 6 public String login () throws Exception {7 // 1. obtain the form's request parameter 8 String username = ServletActionContext. getRequest (). getParameter ("username"); 9 System. out. println (username); 10 11 // 2. access Attribute12 ServletActionContext of the request. getRequest (). setAttribute ("company", "haha"); 13 14 // 3. access the Attribute15 ServletActionContext of the session. getRequest (). getSession (). setAttribute ("age", 40); 16 17 // 4. access Attribute18 ServletActionContext of the application. getServletContext (). setAttribute ("info", "you can go on a appointment after class today"); 19 20 return SUCCESS; 21} 22}
Next I will write a third method to access servlet, from jsp to struts. xml in the Action class, and finally display the results on the page for everyone to learn:
Jsp interface:
Start. jsp
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%>
Struts. xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <package name="default" extends="struts-default"> 7 <action name="hello" class="com.guigu.struts.action2.LoginAction3" method="login"> 8 <result>/result.jsp</result> 9 </action> 10 </package>11 </struts>
result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
The running result is as follows:
When you click Submit
Data has been forwarded from the Action class to the interface.
This article is here. You are welcome to give us some advice and comments!