Accessing WEB resources in Action in Struts2

Access WEB resources in Action  
1. What are WEB resources?
   HttpServletRequest, HttpSession, ServletContext and other native Servlet APIs
Second, why access WEB resources?
   The Controller of the B\S application must access WEB resources
   to read and write attributes in the domain object, read and write cookies, and obtain realPath.
3. How to access?      
   1. The way of decoupling from the Servlet API: Only limited Servlet API objects can be accessed, and only limited methods can be accessed. (Read the request parameters, read and write the properties of the domain object, and invalidate the session)
        For details, please refer to the blog post: Session of Struts2
    (1) Use ActionContext 
    to import com.opensymphony.xwork2.ActionContext;
    (2) Implement the XxxAware interface       
    through Action as follows interface
   
import org.apache.struts2.dispatcher.SessionMap;
	import org.apache.struts2.interceptor.ApplicationAware;
	import org.apache.struts2.interceptor.SessionAware;

    (3) Recommendations for selection: If there are multiple action methods in an Action class, and multiple methods need to use the Map or parameters of the domain object, it is recommended to use the Aware interface
    (4) The Map corresponding to the session is actually SessioMap If the invalidate() method is called after the forced conversion, the session can be invalidated.
   2. The way of coupling with Servlet API: more Servlet API objects can be accessed, and original ecological methods can be called. 
    (1) Use ServletActionContext
     
public String execute(){
		//ServletActionContext: You can get all the Servlet API related objects needed by the current Action object from it
		//1、获取HttpServletRequest:ServletActionContext.getRequest();
		//2、获取HttpSession:ServletActionContext.getRequest().getSession();
		//3. Get ServletContext
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=ServletActionContext.getRequest().getSession();
		ServletContext servletContext=ServletActionContext.getServletContext();
		
		System.out.println("execute...");
		return "success";
	}

    (2) Implement the ServletXxxAware interface
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

//By implementing the ServletXxxAware interface, the required Servlet-related objects can be injected by Struts2
//ServletRequestAware: inject HttpServletRequest object (more commonly used)
//ServletContextAware: inject ServletContext object (more commonly used)
//ServletResponseAware: inject HttpServletResponse object

public class TestServletAware implements ServletRequestAware,ServletContextAware,ServletResponseAware{

	public void setServletRequest(HttpServletRequest request) {
		System.out.println(request);
	}
	private ServletContext context;
	public void setServletContext(ServletContext context) {
		System.out.println(context);
		this.context=context;
	}

	public void setServletResponse(HttpServletResponse response) {
		System.out.println(response);
	}
	public String execute(){
		System.out.println("ServletContext:" + context);
		return "success";
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326446733&siteId=291194637