Struts2访问request,session,application的四种方式
  YWJJl4XSAU9Q 2023年11月02日 119 0


方式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();
	}
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论