ActionContext和ServletActionContext
来源:互联网 发布:java 图形验证码 流程 编辑:程序博客网 时间:2024/06/05 22:43
ActionContext:
ActionContext是一个Action的上下文对象,Action运行期间所用到的数据都保存在ActionContext中(如Session,客户端提交的参数等信息)。
jsp页面提交的参数以map的形式保存在context的requst中,通过getParameter(name)存入。参数为存入参数的名字。
一般情况,我们的ActionContext都是通过:ActionContext context = (ActionContext) actionContext.get();来获取的.我们再来看看这里的actionContext对象的创建:
static ThreadLocal actionContext = new ActionContextThreadLocal();
ActionContextThreadLocal是实现ThreadLocal的一个内部类.ThreadLocal可以命名为"线程局部变量",它为每一个使用该变量的线程都提供一个变量值的副本,使每一个线程都可以独立地改变自己的副本, 而不会和其它线程的副本冲突.这样,我们ActionContext里的属性只会在对应的当前请求线程中可见,从而保证它是线程安全的.
常用的方法:
ActionContext获得的都是Map类型的:
public Object get(Object key) //通过参数key来查找当前ActionContext中的值 public Map getApplication() //返回一个Application级的Map对象 public Static ActionContext getContext() //获得当前线程的ActionContext对象 public Map getParameters() //返回一个包含所有HttpServletRequest参数信息的Map对象 public Map getSession() //返回一个Map类型的HttpSession对象 public void put(Object key,Object value) //向当前ActionContext对象中存入名值对信息 public void setApplication(Map application) //设置Application上下文 public void setSession(Map session) //设置一个Map类型的Session值
(一)设置request、session和application对象:
ActionContext context = ActionContext.getContext();//得到Action上下文信息//context是一个线程安全的,因为不同的用户登录要存入的数据是不一样的。Map request = (Map)context.get("request"); //得到HttpServletReqest的map对象Map session = context.getSession(); //得到HttpSession的Map对象Map application = context.getApplication(); //得到ServletContext的Map对象request.put("msg", "欢迎登陆");session.put("user", user);//在session中保存user对象 application.put("counter", count);
EL表达式读取:
<body> ${sessionScope.user.username},${requestScope.msg},${applicationScope.counter}</body>
(二)直接使用ActionContex类的put()方法
ActionContext.getContext().put("test", "测试");
然后在结果页面中,从请求对象中取出test属性,如下:
${requestScope.test} 或者 <%=request.getAttribute("test")%>
ServletActionContext:
ServletActionContext(com.opensymphony.webwork. ServletActionContext),这个类直接继承了我们上面介绍的ActionContext,
它提供了直接与JavaServlet相关对象访问的功能,它可以取得的对象有:
ServletActionContext获得的都是真实对象。
//HTTPservlet请求对象javax.servlet.http.HttpServletRequest:HttpServletRequest request = ServletActionContext. getRequest(); javax.servlet.http.HttpServletResponse //HTTPservlet相应对象//Servlet上下文javax.servlet.ServletContext:ServletContext context = ServletActionContext.getServletContext();javax.servlet.ServletConfig //Servlet配置对象javax.servlet.jsp.PageContext //Http页面上下文
从ServletActionContext里取得Servlet的相关对象:
<1>取得HttpServletRequest对象:
HttpServletRequest request = ServletActionContext. getRequest();
<2>取得HttpSession对象:
HttpSession session = ServletActionContext. getRequest().getSession();
ActionContext.getContext()会把HttpServletActionContext中的内容全部复制进去。
public static void setRequest(HttpServletRequest request) { ActionContext.getContext().put(HTTP_REQUEST, request); }
例子:
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方式(自动装配):
要使用IoC方式,我们首先要告诉IoC容器(Container)想取得某个对象的意愿,通过实现相应的接口做到这点。
public class UserAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware { private HttpServletRequest request; private HttpServletResponse response; private HttpSession session; private ServletContext application; public void setServletRequest(HttpServletRequest request) { this.request = request; this.session = request.getSession(); this.application = session.getSerlvetContext(); } public void setServletResponse(HttpServletResponse response) { this.response = response; } public String execute() { HttpSession session = request.getSession(); return SUCCESS; }}
ActionContext中可以继承RequstAware,SessionAware,ApplicationAware,重写方法来自动装配。
- ActionContext 和 ServletActionContext区别
- ActionContext和ServletActionContext小结
- ActionContext和ServletActionContext
- ActionContext和ServletActionContext介绍
- ActionContext和ServletActionContext小结
- ActionContext和ServletActionContext小结
- ActionContext和ServletActionContext
- ActionContext和ServletActionContext小结
- ActionContext和ServletActionContext小结
- ActionContext和ServletActionContext小结
- ActionContext和ServletActionContext小结
- ActionContext和ServletActionContext小结
- ActionContext和ServletActionContext小结
- ActionContext和ServletActionContext小结
- ActionContext和ServletActionContext小结
- ActionContext 和 ServletActionContext对比
- ActionContext和ServletActionContext小结
- ActionContext和ServletActionContext小结
- 2月份目标完成情况
- 第一章 MQ基础概念和介绍
- Makefile 学习
- JSP九大内置对象及四个作用域
- IPVS源代码分析---备份的实现
- ActionContext和ServletActionContext
- 西南地区ArcGIS、ENVI产品咨询
- 计算机硬件方面简单介绍
- 在webstorm中使用git
- Python 提取文件,备份,压缩,并上传到远程FTP后,清空指定时间范围外的文件
- 题目1083:特殊乘法
- sqlserver2005移动master位置
- 如何防范html5移动开发中会遇到的安全问题
- 大规模集成电路机