一、ActionContext
ActionContext(com.opensymphony.xwork.ActionContext)是Action执行时的上下文,上下文可以看作是一个容器(其实我们这里的容器就是一个Map而已),它存放的是Action在执行时需要用到的对象。ActionContext对象的创建过程是static ThreadLocal actionContext = new ActionContextThreadLocal()。ActionContextThreadLocal是实现ThreadLocal的一个内部类。这样ActionContext里的属性只会在对应的当前请求线程中可见,从而保证它是线程安全的。
通过ActionContext取得相关对象:ActionContext.getContext().getSession();。
二 、ServletActionContext
ServletActionContext该类直接继承了ActionContext,提供直接与Servlet相关对象访问的功能,故ServletActionContext也线程安全。可取得对象:
1 javax.servlet.http.HttpServletRequest:HTTPservlet请求对象
2 javax.servlet.http.HttpServletResponse:HTTPservlet响应对象
3 javax.servlet.ServletContext:Servlet上下文信息
4 javax.servlet.ServletConfig:Servlet配置对象
5 javax.servlet.jsp.PageContext:Http页面上下文
从ServletActionContext里取得Servlet的相关对象
取得HttpServletRequest对象: HttpServletRequest request = ServletActionContext. getRequest();
取得HttpSession对象: HttpSession session = ServletActionContext. getRequest().getSession();
三 、ServletActionContext和ActionContext联系
ServletActionContext和ActionContext存在着一些重复的功能。遵循的原则是:ActionContext能够实现我们的功能,最好就不要使用ServletActionContext,让Action尽量不要直接去访问Servlet的相关对象。
四 、注意点
不要在Action的构造函数里使用ActionContext.getContext(),因为此时ActionContext里的一些值也许没有设置,这时通过ActionContext取得的值也许是null。同样HttpServletRequest req = ServletActionContext.getRequest()也不要放在构造函数中,也不要直接将req作为类变量给其赋值。原因是static ThreadLocal actionContext = new ActionContextThreadLocal()可以看出ActionContext是线程安全的,而ServletActionContext继承自ActionContext故ServletActionContext也线程安全。线程安全要求每个线程都独立进行,故req的创建也要求独立进行,故ServletActionContext.getRequest()不要放在构造函数中,也不要直接放在类中,而应放在每个具体方法体中如login(),queryAll(),insert()等,才能保证每次产生对象时独立的建立了一个req。
五 、struts2中获得request、response和session
(1)非IOC方式
方法一:
使用org.apache.struts2.ActionContext类,通过它的静态方法getContext()获取当前Action的上下文对象。
ActionContext ctx = ActionContext.getContext();
ctx.put("liuwei", "andy");
Map session = ctx.getSession(); //session
HttpServletRequest request = ctx.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
HttpServletResponse response = ctx.get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE); 细心的朋友可以发现这里的session是个Map对象, 在Struts2中底层的session都被封装成了Map类型. 我们可以直接操作这个Map对象进行对session的写入和读取操作, 而不用去直接操作HttpSession对象.
方法二:
使用org.apache.struts2.ServletActionContext类
public class UserAction extends ActionSupport {
//其他代码片段
private HttpServletRequest req;
// private HttpServletRequest req = ServletActionContext.getRequest(); 这条语句放在这个位置是错误的,同样把这条语句放在构造方法中也是错误的。
public String login() {
req = ServletActionContext.getRequest(); //req的获得必须在具体的方法中实现
user = new User();
user.setUid(uid);
user.setPassword(password);
if (userDAO.isLogin(user)) {
req.getSession().setAttribute("user", user);
return SUCCESS;
}
return LOGIN;
}
(2)IoC方式(即使用Struts2 Aware拦截器)
要使用IoC方式,我们首先要告诉IoC容器(Container)想取得某个对象的意愿,通过实现相应的接口做到这点。
public class UserAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {
private HttpServletRequest request;
rivate HttpServletResponse response;
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public String execute() {
HttpSession session = request.getSession();
return SUCCESS;
}}