struts2的Action放弃了request,response等ServletAPI,使得在业务层上更加独立,在有时候使用struts2进行Web开发的时候,不可避免的要在action中使用servletAPI,那么如何在Action中获取并使用servletAPI呢,
-
通过ActionContext类的静态方法获取(非IOC方式)
package com.wang.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 通过actioncontext类访问并获取servletAPI
* 其对象是一个map类型的
* @author Administrator
*/
public class ContextAction extends ActionSupport{
public String execute() throws Exception {
String name="zhangSan";
//获取seesion
Map session = ActionContext.getContext().getSession();
//把想存的值放到session域中
session.put("sessionName", name);
//获取request
Map request=(Map)ActionContext.getContext().get("request");
//把想存的值存入request域中
request.put("requestName", name);
//获取application
Map application = ActionContext.getContext().getApplication();
//把需要的值存入application域中
application.put("applicationName", name);
return null;
};
}
上面获取到的三大域都是Map类型的,比如对于request来说,我们通常喜欢获取HttpServletRequest类型的,因为这个类型的request功能要强大得多,用ActionContext也是可以获取的,而且也是我们更常用的一种方式,如下:
//获取HttpServletRequest
HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
//通过request获取sesssion
HttpSession session = request.getSession();
-
通过ServletActionContext的静态方法获取(非IOC方式)
package com.wang.action; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.apache.struts2.StrutsStatics; import org.apache.struts2.dispatcher.ng.servlet.StrutsServlet; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; /** * 通过ServletActionContext类访问并获取servletAPI * 虽然是使用了静态方法获取request,response,但仍能保证获取到当前用户,当前线程的request,response, * 不会反生混淆,因为struts2利用ThreadLoca来维护当前线程的servlet对象,是线程安全的 * @author Administrato */ public class ContextAction2 extends ActionSupport{ public String execute() throws Exception { String name="zhangSan"; //获取HttpServletRequest HttpServletRequest request = ServletActionContext.getRequest(); //获取session HttpSession session = request.getSession(); return null; };
-
实现相关的Aware接口(IOC方式)
- request 对应ServletRequestAware接口
- response对应ServletResponseAware接口
- session 对应SessionAware接口
- application对应ServletContextAware接口
struts2在实例化一个action对象时,如果发现他实现了相关的Aware接口,就会把相应的资源通过Aware接口方法注入进去,所以叫做注入方式(IOC方式),以request为例,实现了ServletRequestAware接口,就会有一个setServletRequest()方法获取request对象,Aware接口实际上是一种拦截器,他会在执行Action前执行,把相关的servlet对象设置进来,代码如下:
package com.wang.action;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.ng.servlet.StrutsServlet;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ContextAction1 extends ActionSupport implements ServletRequestAware {
HttpServletRequest request;
public String execute() throws Exception {
String name="zhangSan";
request.setAttribute("name", name);
return null;
}
@Override
public void setServletRequest(HttpServletRequest request) {
//通过该方法可直接获取到request对象
this.request=request;
};
}