关于ActionContext.getContext()
2016-04-16 16:34
267 查看
为了避免与Servlet API耦合在一起,方便Action类做单元测试,Struts 2对HttpServletRequest、HttpSession和ServletContext进行了封装,构造了三个Map对象来替代这三种对象,在Action中,直接使用HttpServletRequest、HttpSession和ServletContext对应的Map对象来保存和读取数据。
(一)通过ActionContext来获取request、session和application对象的LoginAction1
[java] view
plain copy
ActionContext context = ActionContext.getContext();
Map request = (Map)context.get("request");
Map session = context.getSession();
Map application = context.getApplication();
request.put("greeting", "欢迎您来到程序员之家");//在请求中放置欢迎信息。
session.put("user", user);//在session中保存user对象
application.put("counter", count);
在JSP中读取
[xhtml] view
plain copy
<body><h3>${sessionScope.user.username},${requestScope.greeting}。<br>本站的访问量是:${applicationScope.counter}</h3>
</body>
(二)直接使用ActionContex类的put()方法
ActionContext.getContext().put("greeting", "欢迎您来到http://www.
sunxin.org");
然后在结果页面中,从请求对象中取出greeting属性,如下:
${requestScope.greeting} 或者 <%=request.getAttribute("greeting")%>
(一)通过ActionContext来获取request、session和application对象的LoginAction1
[java] view
plain copy
ActionContext context = ActionContext.getContext();
Map request = (Map)context.get("request");
Map session = context.getSession();
Map application = context.getApplication();
request.put("greeting", "欢迎您来到程序员之家");//在请求中放置欢迎信息。
session.put("user", user);//在session中保存user对象
application.put("counter", count);
在JSP中读取
[xhtml] view
plain copy
<body><h3>${sessionScope.user.username},${requestScope.greeting}。<br>本站的访问量是:${applicationScope.counter}</h3>
</body>
(二)直接使用ActionContex类的put()方法
ActionContext.getContext().put("greeting", "欢迎您来到http://www.
sunxin.org");
然后在结果页面中,从请求对象中取出greeting属性,如下:
${requestScope.greeting} 或者 <%=request.getAttribute("greeting")%>
相关文章推荐
- Node.js 从入门到茫然系列——入门篇
- MFC改变自定义鼠标图标的大小
- 【BZOJ 1726】【Usaco 2006 Nov】Roadblocks 次短路
- 用swift开发仪表盘控件(一)
- 电子商务大赛
- 史上最详细bitbucket入门手册,手把手操作指南-----可建立私有仓库
- HDU1016 Prime Ring Problem (回溯 + 剪枝)
- 移动端触屏滑动,JS事件
- python学习笔记三:解析html(HTMLParser、SGMLParser),并抓取图片
- 欢迎使用CSDN-markdown编辑器
- uvalive5848
- Android ListView控件选中item项,并保持选中状态
- 腾讯2面
- Win10下配置Hyper-V虚拟机通过NAT或桥接方式联网
- C++中的explicit关键字介绍
- mysql索引总结----mysql 索引类型以及创建
- Java 读写Properties配置文件
- 数据结构和算法12 之希尔排序
- ubuntu14.04截图工具ksnapsshot
- C++中的explicit关键字介绍