在struts2的action中有時會需要訪問request,session以及applicationContext等web資源,這裡我總結了四種可行的方法,供讀者參考。

(1)使用ActionContext

<pre name="code" class="java"><pre name="code" class="html">package com.jiajia.actions;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

public class TestActionContext {

	public String execute(){
		//獲取actionContext物件
		ActionContext context = ActionContext.getContext();
		
		//ActionContext中獲取的application是一個Map,裡面可以儲存需要的引數。
		Map<String, Object> application = context.getApplication();
		//在application中放置一個引數applicationAttr
		application.put("applicationAttr",
				"通過ActionContext的getApplication()方法獲取application");
		
		//ActionContext中獲取的session也是一個Map
		Map<String, Object> session = context.getSession();
		//在session中放置一個引數
		session.put("sessionAttr",
				"通過ActionContext的getSession()方法獲取session");
		
		//ActionContext中獲取的request也是一個Map,但獲取方法有別於session和application
		//ActionContext中並沒有getRequest()方法(我還不理解為什麼會這樣)
		Map<String, Object> request = (Map<String, Object>) context.get("request");
		//在request中放置一個引數
		request.put("requestAttr",
				"通過ActionContext的get(\"request\")方法獲取request");
		
		return "success";
	}
}


(2)使用ServletActionContext
<pre name="code" class="java">package com.jiajia.actions;

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

import org.apache.struts2.ServletActionContext;

/**
 * @author jiajia
 * 該類主要用來測試通過ServletActionContext類來
 * 直接獲取request、session以及application的方法
 */
public class TestServletActionContext {

	public String execute(){

		ServletContext application = ServletActionContext.getServletContext();
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();

		//在application中放置一個引數applicationAttr
		application.setAttribute("applicationAttr",
				"通過ServletActionContext.getServletContext()獲取application");

		//在session中放置一個引數
		session.setAttribute("sessionAttr",
				"通過ServletActionContext.getRequest().getSession()獲取session");

		//在request中放置一個引數
		request.setAttribute("requestAttr",
				"通過ServletActionContext.getRequest()獲取request");

		return "success";
	}
}


(3)實現RequestAware, SessionAware, ApplicationAware介面
package com.jiajia.actions;

import java.util.Map;

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

/**
 * @author jiajia
 * 該類主要用來測試通過實現RequestAware, SessionAware, ApplicationAware介面,struts會自動向
 * 物件中注入Map型別的application、session、以及request屬性
 */
public class TestAware implements RequestAware, SessionAware, ApplicationAware{

	Map<String, Object> application;
	Map<String, Object> session;
	Map<String, Object> request;

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

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

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

	public String execute(){
		//在application中放置一個引數applicationAttr
		application.put("applicationAttr",
				"通過實現ApplicationAware介面獲取application");
		
		//在session中放置一個引數
		session.put("sessionAttr",
				"通過實現SessionAware介面獲取session");
		
		//在request中放置一個引數
		request.put("requestAttr",
				"通過實現RequestAware介面獲取request");

		return "success";
	}

}
(4)實現ServletRequestAware, ServletContextAware介面
package com.jiajia.actions;

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

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;

public class TestServletAware implements ServletRequestAware,
		ServletContextAware {
	
	ServletContext application;
	HttpServletRequest request;
	HttpSession session;

	@Override
	public void setServletContext(ServletContext arg0) {
		this.application = arg0;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}

	public String execute(){

		session = request.getSession();
		
		//在application中放置一個引數applicationAttr
		application.setAttribute("applicationAttr",
				"通過實現ServletContextAware介面獲取application");

		//在session中放置一個引數
		session.setAttribute("sessionAttr",
				"通過實現ServletRequestAware介面獲取request再呼叫getSession()獲取session");
		//在request中放置一個引數
		request.setAttribute("requestAttr",
				"通過實現ServletRequestAware介面獲取request");

		return "success";
	}
}

上述四種方法中(1)和(3)與原始Servlet API是沒有耦合的,但功能也相對較弱,(2)和(4)與原始Servlet API是緊密耦合的,可以提供完整的servlet中的功能

完整示例程式碼:http://download.csdn.net/detail/jiajia333666/9050793

.