1. ActionContext
在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息,甚至需要直接对JavaServlet Http的请求(HttpServletRequest),响应(HttpServletResponse)操作. 我们需要在Action中取得request请求参数"username"的值:
- ActionContext context = ActionContext.getContext();
- Map params = context.getParameters();
- String username = (String) params.get("username");
ActionContext(com.opensymphony.xwork.ActionContext)是Action执行时的上下文,上下文可以看作是一个容器(其实我们这里的容器就是一个Map而已),它存放的是Action在执行时需要用到的对象. 一般情况, 我们的ActionContext都是通过: ActionContext context = (ActionContext) actionContext.get();来获取的.我们再来看看这里的actionContext对象的创建:
static ThreadLocal actionContext = new ActionContextThreadLocal();
ActionContextThreadLocal是实现ThreadLocal的一个内部类.ThreadLocal可以命名为"线程局部变量",它为每一个使用该变量的线程都提供一个变量值的副本,使每一个线程都可以独立地改变自己的副本,而不会和其它线程的副本冲突.这样,我们ActionContext里的属性只会在对应的当前请求线程中可见,从而保证它是线程安全的.
通过ActionContext取得HttpSession: Map session = ActionContext.getContext().getSession();
2. ServletActionContext
ServletActionContext(com.opensymphony.webwork. ServletActionContext),这个类直接继承了我们上面介绍的ActionContext,它提供了直接与Servlet相关对象访问的功能,它可以取得的对象有:
(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的相关对象:
<1>取得HttpServletRequest对象: HttpServletRequest request = ServletActionContext. getRequest();
<2>取得HttpSession对象: HttpSession session = ServletActionContext. getRequest().getSession();
3. ServletActionContext和ActionContext联系
ServletActionContext和ActionContext有着一些重复的功能,在我们的Action中,该如何去抉择呢?我们遵循的原则是:如果ActionContext能够实现我们的功能,那最好就不要使用ServletActionContext,让我们的Action尽量不要直接去访问Servlet的相关对象.
注意:在使用ActionContext时有一点要注意: 不要在Action的构造函数里使用ActionContext.getContext(),因为这个时候ActionContext里的一些值也许没有设置,这时通过ActionContext取得的值也许是null;同样,HttpServletRequest req = ServletActionContext.getRequest()也不要放在构造函数中,也不要直接将req作为类变量给其赋值。至于原因,我想是因为前面讲到的static ThreadLocal actionContext = new ActionContextThreadLocal(),从这里我们可以看出ActionContext是线程安全的,而ServletActionContext继承自ActionContext,所以ServletActionContext也线程安全,线程安全要求每个线程都独立进行,所以req的创建也要求独立进行,所以ServletActionContext.getRequest()这句话不要放在构造函数中,也不要直接放在类中,而应该放在每个具体的方法体中(eg:login()、queryAll()、insert()等),这样才能保证每次产生对象时独立的建立了一个req。
4. struts2中获得request、response和session
(1)非IoC方式
方法一:使用org.apache.struts2.ActionContext类,通过它的静态方法getContext()获取当前Action的上下文对象。
- ActionContext ctx = ActionContext.getContext();
- ctx.put("liuwei", "andy"); //request.setAttribute("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;
- }
- public String queryAll() {
- req = ServletActionContext.getRequest(); //req的获得必须在具体的方法中实现
- uList = userDAO.queryAll();
- req.getSession().setAttribute("uList", uList);
- return SUCCESS;
- }
- //其他代码片段
- }
- IoC方式(即使用Struts2 Aware拦截器)
- 要使用IoC方式,我们首先要告诉IoC容器(Container)想取得某个对象的意愿,通过实现相应的接口做到这点。
- public class UserAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {
- private HttpServletRequest request;
- private 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;
- }
- }
相关推荐
struts2文件上传,与xml校验 struts2文件校验可通过xml配对action的名称去校验属性的值。例如UploadAction.java中有一个book的属性。那么我可以定义一个UploadAction-validation.xml去校验这个book属性的内容。 xml的...
struts2 chm 程序包 org.apache.struts2 接口概要 接口 说明 StrutsStatics Constants used by Struts. 类概要 类 说明 RequestUtils Request handling utility class. ServletActionContext Web-specific ...
这种方式主要是利用了com.opensymphony.xwork2.ActionContext类以及org.apache.struts2.ServletActionContext类,具体的方法如下所示。 获得request对象: A . HttpServletRequest request = ServletActionContext....
1.struts2 带进度条文件上传 源码 2.源码中有lib 下载后部署马上就能用 3.代码中有详细的注释信息 4.对关键点有详细的解释说明 5.如果谁用起来觉得有问题可在评论留言,留联系方式,我会联系你的 6.非常感谢原创作者...
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { // 上传文件存放路径 private final static String ...
作者处处从实战出发,在丰富的示例中直观地探讨了许多实用的技术,如数据类型转换、文件上传和下载、提高Struts 2应用的安全性、调试与性能分析、FreeMarker、Velocity、Ajax,等等。跟随作者一道深入Struts 2,聆听...
HttpServletRequest request = ServletActionContext.getRequest (); String sourceLan = request.getParameter("source"); String targetLan = request.getParameter("target"); String q = request....
2:在页面中实现自动增加的序号用iterator的statuts的index属性 eg: <s:iterator value="#request.inOutAccountList" id="data" status="listStat"> <s:property value="#listStat.index+1"/> </s:iterator> ...
org.apache.struts2.ServletActionContext (implements org.apache.struts2.StrutsStatics) org.apache.struts2.RequestUtils org.apache.struts2.StrutsConstants java.lang.Throwable (implements java.io....
3、starts2的使用思路 1、jar 2、配置文件 3、常用类 servletActionContext 4、访问流程 url -> filter -> struts.xml -> package -> action -> name="userAction_*" -> class -> method="{1}" 5、核心struts.xml...
书中介绍了如何利用Struts 2 来解决Web 应用开发中的常见问题,同时还深入浅出地探讨了许多能帮助程序员编写Struts 2 应用程序的技巧,如管理页面导航活动、输入验证、国际化和本地化、对Ajax 的支持,等等。书中...
struts2.0 详细介绍了struts.xml的配置,以及web.xml的配置和ActionContext,ServletActionContext的用法和区别。ActionContext主要用于设置属性,而ServletActionContext主要用来得到属性
主要实现了用户的登录系统..并且验证了struts中的跳转..还有跳转到另一个action中的类型.....还有struts与servlet松耦合的ActionContext的使用..并且从request域中,session域和application整个应用程序域中获取信息..
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class DownloadAction extends ActionSupport { /** * */ private static final long ...
import org.apache.struts2.ServletActionContext; import sunyang.functions.domain.Function; import sunyang.functions.service.IFunctionFacade; import sunyang.module.domain.Module; import ...
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionSupport; public class DownLoadPhoneFile extends ActionSupport { //
getServletContext()空指针异常的原因getServletContext()空指针异常的原因getServletContext()空指针异常的原因getServletContext()空指针异常的原因getServletContext()空指针异常的原因
1). Action 类的要求 2). ActionSupport 3)、在 Action 中如何访问 WEB 资源: ①. 通过 ActionContext ②. 通过 Aware 接口 ③. 通过 ServletActionContext