waitingfortime

导航

struts2 -- 在action中获取scope对象的4种方式

编辑器加载中... struts2中将action与servlet解耦合,故我们在action中的excute方法中无法直接使用request、session以及application对象,为了操作这些对象,我们必须编写另外一些代码,现将在action中访问scope对象的方式总结如下,共四种。

方式一: 与Servlet解耦合的非IOC方式

特点:获取的scope对象与容器无关,通过ActionContext获取

1 import java.util.Map;
2
3  import com.opensymphony.xwork2.ActionContext;
4  import com.opensymphony.xwork2.ActionSupport;
5
6  /**
7 * @author waitingfortime
8 *
9 */
10  public class test2Action extends ActionSupport {
11 private String username;
12 ActionContext context;
13 Map request;
14 Map session;
15 Map application;
16 public String execute() throws Exception {
17 context=ActionContext.getContext();//从ActionContext中获得上下文对象,并从该对象中一次获得scope对象
18 request=(Map) context.get("request");
19 session=context.getSession();
20 application=context.getApplication();
21
22 request.put("req", "requst属性");
23 session.put("ses", "sesion属性");
24 application.put("app", "application属性");
25 return SUCCESS;
26 }
27 }

 在jsp页面中有2种方式获得scope对象的值

${requestScope.req}
${sessionScope.ses}
${applicationScope.app}
<h4>以下使用scope.getAttribute的形式来接受</h4>
	request:	<%=request.getAttribute("req") %><br>
	session:	<%=session.getAttribute("ses") %><br>
	application:<%=application.getAttribute("app") %><br>

分析:通过ActionContext的getContext静态方法得到ActionContext对象,然后ActionContext对象调用get方法来获取一个存储在request范围中的对象。我们使用el或通过request.getAttribute这样的方式均可以获取对象值,这说明了这些Map request对象实际是存储在request范围内的对象。

方式二、与Servlet解耦合的IOC方式

public class test3Action extends ActionSupport implements RequestAware,SessionAware,ApplicationAware {
	private String username;
	Map request;
	Map session;
	Map application;
	public String execute() throws Exception {
		request.put("req", "requst属性");
		session.put("ses", "sesion属性");
		application.put("app", "application属性");
		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;
	}
	...省略username的get/set方法	
}

在方式二中它是通过实现接口,在重写接口中的方法中完成对Map requset的赋值,所以称之IOC方式。

方式三、与Servlet耦合的非IOC方式

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author waitingfortime
 */
public class test2Action extends ActionSupport {
	private String username;
	HttpServletRequest request;
	HttpSession session;
	ServletContext application;
	public String execute() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		application = ServletActionContext.getServletContext();

		request.setAttribute("req", "requst属性");
		session.setAttribute("ses", "sesion属性");
		application.setAttribute("app", "application属性");
		return SUCCESS;
	}

此方法获取的纯粹的Scope对象,它与容器相关,这些Scope对象操作更强。

方式四、与Servlet耦合的IOC方式

public class Login4Action extends ActionSupport implements ServletRequestAware,ServletContextAware{
	private String username;
	ActionContext context;
	HttpServletRequest request;
	HttpSession session;
	ServletContext application;
	public String execute() throws Exception {
		context=ActionContext.getContext();
		session=request.getSession();	
		request.setAttribute("req", "requst属性");
		session.setAttribute("ses", "sesion属性");
		application.setAttribute("app", "application属性");
		return SUCCESS;
	}
	
	public void setServletRequest(HttpServletRequest request) {
		System.out.println("测试:"+request);
		this.request=request;
	}
	public void setServletContext(ServletContext application) {
		System.out.println("测试:"+application);
		this.application=application;
	}
	...省略username的get/set方法
}

方式四则是实现了接口,来通过依赖注入得到对象。

posted on 2011-03-26 09:52  waitingfortime  阅读(2902)  评论(0编辑  收藏  举报