Preface
I have introduced you to the introductory tutorial about Struts2. I will continue to share the knowledge I have learned, the problems I encountered during the learning process and solutions. Of course, if readers find any problems, they can inform me in the comments below. Thank you in advance
Accessing WEB resources in Action
web resources
The so-called WEB resource refers to native APIs such as HttpServletRequest, HttpServletResponse, and ServletContext. As a B/S application development controller, it must be able to access WEB resources, such as reading and writing properties to domain objects.
How to access WEB resources
Decoupling method from ServletAPI: In order to avoid coupling with ServletAPI and facilitate unit testing in Action, Struts2 encapsulates HttpServletRequest, HttpSession and ServletContext, constructs 3 Map objects to replace these 3 objects. In Action, you can use the corresponding Map objects of HttpServletRequest, HttpServletSession, and HttpServletContext to save and read data.
public Object get(Object key) method to pass the request parameter to achieve the implementation.setRequest() method, so that the requestMap can be called in all action methods. Use ActionContext to get a code demonstration of a domain object
Send the request from the index.jsp page to showPage.jsp and hand the request over to Struts' Action class ObjectAction.java to handle the request
In ObjectAction.java, the domain object is retrieved by ActionContext and the property value is added to it, and the request is forwarded to showPage.jsp
On the showPage.jsp page, we can get the values of each domain object
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>Handle</title></head><body>-- Pass the name parameter, which can be obtained from the Params domain object in the Action class to test the Params domain object<a href="showPage.action?name=bgZyy" rel="external nofollow" >Application</a></body></html>
ObjectAction.java
package com.request.test;import com.opensymphony.xwork2.ActionContext;import java.util.Map;public class ObjectAction { public String print() {// Get ActionContext ActionContext actionContext = ActionContext.getContext();// Get the Map corresponding to the action domain object Map<String, Object> applicationMap = actionContext.getApplication();// Assign applicationMap.put("applicationMap", "applicationMapValue");// Get the Map corresponding to Session Map<String, Object> sessionMap = actionContext.getSession();// Assign sessionMap to sessionMap.put("sessionMap", "sessionMapValue");// Get the Map corresponding to request, there is no corresponding getRequest() method, so use the get("request") method Map<String, Object> requestMap = (Map<String, Object>) actionContext.get("request");// Get the Map corresponding to request. There is no corresponding getRequest() method, so use the get("request") method Map<String, Object> requestMap = (Map<String, Object>) actionContext.get("request");// Be requestMap Assign requestMap.put("requestMap", "requestMapValue");// Get the Map corresponding to params Map<String, Object> paramsMap = actionContext.getParameters();// Get the parameter value of params String[] params = (String[]) paramsMap.get("name");// Print the parameter value to the console System.out.println(params[0]); return "success"; }}struts.xml (only display the package node)
<package name="showValue" extends="struts-default"> <action name="showPage" method="print"> <result name="success">/showPage.jsp</result> </action></package>
showPage.jsp (only displays body tag content)
<%--Get the value of the application domain object--%>${applicationScope.applicationMap}<br><%-Get the value of the session domain object--%>${sessionScope.sessionMap}<br><%-Get the value of the request domain object--%>${requestScope.requestMap}<br><%-Get the value of the request domain object--%>${requestScope.requestMap}Details and attention to (drag the image to the new tab page of the browser to view the larger image)
Accessing WEB resources using the XxxAware interface
Like using ActionContext, sending a request to ObjectAction.java on the index.jsp page is processed by the print2() method.
Assign a value to the domain object in print2() method in ObjectAction.java and get the value of the passed parameter
Finally, we get the value of the domain object in showPage.jsp. In order to distinguish from the previous one, we add symbols to each output value to distinguish.
index.jsp (displays body tag part)
<a href="showPage2.action?name=smZyy" rel="external nofollow" >ToPage2</a>
ObjectAction.java
package com.request.test;import com.opensymphony.xwork2.ActionContext;import org.apache.struts2.interceptor.ApplicationAware;import org.apache.struts2.interceptor.ParameterAware;import org.apache.struts2.interceptor.RequestAware;import org.apache.struts2.interceptor.SessionAware;import java.util.Map;public class ObjectAction implements RequestAware, SessionAware, ApplicationAware, ParameterAware{ private Map<String, Object> requestMap; private Map<String, Object> sessionMap; private Map<String, Object> applicationMap; private Map<String, String[]> parameterMap;// showPage.action Execute the required method public String print2() { applicationMap.put("applicationMap", "applicationMapVal==="); requestMap.put("requestMap", "requestMapVal==="); sessionMap.put("sessionMap", "sessionMapVal==="); String[] name = parameterMap.get("name"); System.out.println(name[0] + "===="); return "success"; } @Override public void setApplication(Map<String, Object> map) { this.applicationMap = map; } @Override public void setParameters(Map<String, String[]> map) { this.parameterMap = map; } @Override public void setRequest(Map<String, Object> map) { this.requestMap = map; } @Override public void setSession(Map<String, Object> map) { this.sessionMap = map; }}struts.xml (displays the package node part)
<package name="showValue" extends="struts-default"> <action name="showPage2" method="print2"> <result name="success">/showPage.jsp</result> </action></package>
showPage.jsp (Same as above, it has not changed at all)
Selection suggestions
Through the above two methods of obtaining WEB resources, we can know that if the first method has many action methods that require calling domain objects, each method must be implemented, while the second method only needs to be implemented once, so in actual development, the corresponding method is selected to implement it according to the situation.
Coupled with ServletAPI: More ServletAPIs can be accessed and native methods can be called
The implementation method here is similar to the above implementation method, so I won't talk about it.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.