Four ways to obtain WEB resources in Struts2

Struts2 has four ways to obtain request, session, and application in the Web.


1. Inject the request through the IOC mechanism by implementing the ServletRequestAware interface (this method is the most troublesome and is not recommended);

/**
 * By implementing the ServletXxxAware interface, the required Servlet-related objects can be injected (setter methods) by Struts2
 * ServletRequestAware: inject HttpServletRequest object
 * ServletContextAware: inject ServletContext object (more commonly used)
 * ServletResponseAware: inject HttpServletResponse object
 *
 */
public class TestServletAwareAction implements ServletRequestAware,ServletContextAware,ServletResponseAware{
	
	public String execute() {
		System.out.println("ServletContext:"+context);
		return "success";
	}
	private ServletContext context;
	@Override
	public void setServletContext(ServletContext context) {
		 System.out.println(context);
		 this.context = context;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		System.out.println(request);
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		System.out.println(response);
	}
		
}


2. Implement RequestAware, SessionAware, and ApplicationAware interfaces, and inject three Maps through the IOC mechanism;

public class TestAwareAction  implements ApplicationAware,SessionAware,RequestAware,ParameterAware{
	
	
		public String execute(){
			//1. Add an attribute to the application: applicationKey2 - applicationValue2
			application.put("applicationKey2", "applicationValue2");
			//2. Read an attribute date from application and print
			System.out.println("时间"+application.get("date"));
			
			//The usage of the following methods is the same as above
			session.put("sessionKey2", "sessionValue2");
			request.put("requestKey2", "requestValue2");
			
			//get request parameters
			System.out.println("参数:"+parameters.get("name")[0]);
			
			return "success";
		}
		private Map<String,Object> application;
		private Map<String,String[]> parameters;
		private Map<String,Object> request;
		private Map<String,Object> session;
		
		@Override
		public void setApplication(Map<String, Object> application) {
				this.application = application;
		}


		@Override
		public void setParameters(Map<String, String[]> parameters) {
			this.parameters = parameters;
			
		}


		@Override
		public void setRequest(Map<String, Object> request) {
			this.request = request;
			
		}


		@Override
		public void setSession(Map<String, Object> session) {
			this.session = session;
			
		}
	
	
}


3. Use ServletActionContext class (this method is coupled with Servlet)

/**
 * Get WEB resources by coupling with Servlet API
 * Use ServletActionContext to get all the Servlet API related objects needed by the current Action object.
 * Commonly used methods:
 *  1.获取HttpServletRequest:ServletActionContext.getRequest();
 *  2.获取HttpSession:ServletActionContext.getRequest().getSession();
 *  3.获取ServletContext:ServletActionContext.getServletContext();
 */
public class TestServletActionContextAction {
		
	public String execute() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = ServletActionContext.getRequest().getSession();
		ServletContext servletContext = ServletActionContext.getServletContext();
		System.out.println("execute...");
		return "success";
	}
}

4. Use the ActionContext class (this method is decoupled from the Servlet and is recommended)

/**
 * Obtain WEB resources in a decoupled way
 * Use ActionContext
 *
 */
public class TestActionContextAction {
	
	public String execute() {
		//0. Get the ActionContext object, which is the context object of the Action, from which you can get all the information needed by the current Action
		ActionContext actionContext = ActionContext.getContext();
		
		//1. Get the Map corresponding to the application by calling the getApplication() method of the ActionContext object, and add an attribute to it	
		Map<String,Object> applicationMap = actionContext.getApplication();
		//add property
		applicationMap.put("applicationKey", "applicationValue");
		//get properties
		System.out.println(applicationMap.get("date"));
		
		//2.session
		Map<String,Object> sessionMap = actionContext.getSession();
		sessionMap.put("sessionKey", "sessionValue");
		
		//3.request
		Map<String,Object> requestMap = (Map<String, Object>) actionContext.get("request");
		requestMap.put("requestKey", "requestValue");
		
		//4. Get the Map corresponding to the request parameter and get the specified parameter value
		//Key: the name of the request parameter, value: the string array corresponding to the value of the request parameter
		//Note: ①The return value of getParameters is Map<String,Object>, not Map<String,String[]>
		// ②parameters This Map can only be read and cannot write data
		Map<String,Object> parameters = actionContext.getParameters();
		System.out.println(((String[])parameters.get("name"))[0]);	//atguigu
		return "success";
	}
	
}































Guess you like

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