Struts2访问request,session,application的四种方式

方式1--》访问Map类型的:
package cn.pdsu.edu;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 通过ActionContext获得Map类型的request,session,application
 */
public class ActionDemo1 extends ActionSupport {
	private Map<String,Object> request;
	private Map<String,Object> session;
	private Map<String,Object> application;
	
	public ActionDemo1() {
		this.request = (Map)ActionContext.getContext().get("request");
		this.session = ActionContext.getContext().getSession();
		this.application = ActionContext.getContext().getApplication();
	}

	@Override
	public String execute() throws Exception {
		request.put("message", "message");
		session.put("message", "message");
		application.put("message", "message");
		return SUCCESS;
	}
}



方式2--》访问map类型的:
package cn.pdsu.edu;
import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 通过xxxAware接口来获得Map类型的request,session,application
 */
public class ActionDemo2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
	private Map<String,Object> request;
	private Map<String,Object> session;
	private Map<String,Object> application;

	@Override
	public String execute() throws Exception {
		request.put("message", "message");
		session.put("message", "message");
		application.put("message", "message");
		return SUCCESS;
	}

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

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

	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}
}



方式3--》访问HttpServletRequest

package cn.pdsu.edu;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ActionDemo3 extends ActionSupport {
	private HttpServletRequest request;
	private HttpSession session;
	private ServletContext application;

	public ActionDemo3() {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		application = session.getServletContext();
	}

	@Override
	public String execute() throws Exception {
		request.setAttribute("message", "message");
		session.setAttribute("message", "message");
		application.setAttribute("message", "message");
		return SUCCESS;
	}
}



方式4--》通过接口访问HttpServletRequest
package cn.pdsu.edu;
import java.util.Map;

import javax.management.j2ee.statistics.ServletStats;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ActionDemo4 extends ActionSupport implements ServletRequestAware{
	private HttpServletRequest request;
	private HttpSession session;
	private ServletContext application;

	@Override
	public String execute() throws Exception {
		request.setAttribute("message", "message");
		session.setAttribute("message", "message");
		application.setAttribute("message", "message");
		return SUCCESS;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		this.session = request.getSession();
		this.application = session.getServletContext();
	}
}

你可能感兴趣的:(java,struts2)