通过四种方式访问web页面的范围对象

package com.neusoft.action;


import java.util.Map;


import javax.servlet.http.HttpServletRequest;


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


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


//通过实现RequestAware,SessionAware,ApplicationAware来实现对范围对象的访问,并且要实现一系列的get和set方法..来完成对范围对象的访问
public class WebAction extends ActionSupport implements RequestAware,
		ServletRequestAware {
	private Map<String, Object> request;
	private HttpServletRequest httprequest;


	public String execute() throws Exception {
		@SuppressWarnings({ "unchecked", "static-access" })
		// 通过ActionContext来获取Map<String, Object>类型的范围
		// request,session,application
		Map<String, Object> requestq = (Map<String, Object>) ActionContext
				.getContext().getContext().get("request");
		requestq.put("name", "user");
		request.put("admin", "admin");


		// 通過ServletActionContext來實現對範圍對象的訪問
		HttpServletRequest request1 = ServletActionContext.getRequest();
		request1.setAttribute("maylor", "maylor");


		// 通过实现ServletRequestAware来实现对三大范围对象的访问
		httprequest.setAttribute("terry", "terry");
		return super.execute();
	}


	// public Map<String, Object> getRequest() {
	// return this.request;
	// }


	public void setRequest(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		this.request = arg0;
	}


	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		httprequest = arg0;
	}


}


你可能感兴趣的:(通过四种方式访问web页面的范围对象)