struts2取得request,session,appilcation的方法

Map类型——request,session,application

真实类型——HttpServletRequest,HttpSession,ServletContext

Map类型是Struts对真实类型的一个封装,会将真实类型映射到Map类型中



取得上述元素有以下4种方法

1  通过ActionContext来访问request,session,application对象

2  通过实现RequestAware、SessionAware、ApplicationAware

    接口来访问request,session,application对象(常用)

3  通过ServletActionContext来访问request,session,application对象

4  通过实现ServletRequestAware接口来访问request,session,application对象


第一种方法

package com.bjsxt.struts2.user.action;

import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction1 extends ActionSupport {
 
	private Map request;
	private Map session;
	private Map application;
 
  	public LoginAction1() {
  		request = (Map)ActionContext.getContext().get("request");
  		session = ActionContext.getContext().getSession();
  		application = ActionContext.getContext().getApplication();
  	}
 
  	public String execute() {
  		request.put("r1", "r1");
  		session.put("s1", "s1");
  		application.put("a1", "a1");
  		
  		return SUCCESS;
  	}
}
通过取得Struts的Action的上下文来取得上述三种元素,map类型的。




方法二

package com.bjsxt.struts2.user.action;

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;

public class LoginAction2 extends ActionSupport 
          implements RequestAware,SessionAware,ApplicationAware {
 
	private Map<String, Object> request;
	private Map<String, Object> session;
	private Map<String, Object> application;
 
  // IOC

	public String execute() {
		request.put("r1", "r1");
		session.put("s1", "s1");
		application.put("a1", "a1");
	
		return SUCCESS;
	}

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

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

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


通过Struts注入进来三个元素,map类型。

前台jsp页面获取后台值的方式

<s:property value="#request.r1> | <%=request.getAttribute("r1")%>
<s:property value="#session.s1"/> | <%=session.getAttribute("s1")%>
<s:property value="#application.a1"/> |<%=application.getAttribute("a1")%>



方法三

package com.bjsxt.struts2.user.action;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction3 extends ActionSupport {
 
	private HttpServletRequest request;
	private HttpSession session;
	private ServletContext application; 
	
	public LoginAction3() {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		application = session.getServletContext();
	}
 
	public String execute() {
		request.setAttribute("r1", "r1");
		session.setAttribute("s1", "s1");
		application.setAttribute("a1", "a1");
		return SUCCESS;
	}
}


取得的是真实的类型


方法四
package com.bjsxt.struts2.user.action;

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

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction4 extends ActionSupport implements ServletRequestAware {
 
	private HttpServletRequest request;
	private HttpSession session;
	private ServletContext application;
 
 
	public String execute() {
		request.setAttribute("r1", "r1");
		session.setAttribute("s1", "s1");
		application.setAttribute("a1", "a1");
		
		return SUCCESS;
	}

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


你可能感兴趣的:(struts2取得request,session,appilcation的方法)