关于ActionContext.getContext()的使用方法心得
这个也是我在另外一位仁兄的博客中看到的,原博客的有点长,我把它精简了一下,算看起来比較方便吧。
为了避免与Servlet API耦合在一起,方便Action类做单元測试,Struts 2对HttpServletRequest、HttpSession和ServletContext进行了封装,构造了三个Map对象来替代这三种对象,在Action中,直接使用HttpServletRequest、HttpSession和ServletContext相应的Map对象来保存和读取数据。
(一)通过ActionContext来获取request、session和application对象的LoginAction1
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中读取
<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")%>
下面是原博客的地址,以备查阅http://apps.hi.baidu.com/share/detail/9065250
关于ActionContext.getContext()的使用方法心得的更多相关文章
- 【转载】关于ActionContext.getContext().getParameters()获值问题
ActionContext.getContext().getParameters():一个学员问题的解答 2012-11-12 15:12:05| 分类: 默认分类 | 标签:struts2 ...
- ServletActionContext.getRequest().getSession() 和 ActionContext.getContext().getSession()
ActionContext.getContext().getSession(); 这个方法获取的session是struts封装过的一个Map类型的session,只能调用put()方法缓存数据. S ...
- 关于ActionContext.getContext()的用法心得
转: 为了避免与Servlet API耦合在一起,方便Action类做单元测试,Struts 2对HttpServletRequest.HttpSession和ServletContext进行了封装, ...
- ActionContext.getContext().getSession()
ActionContext.getContext().getSession() 获取的是session,然后用put存入相应的值,只要在session有效状态下,这个值一直可用 ActionConte ...
- 关于ActionContext.getContext()的用法
为了避免与Servlet API耦合在一起,方便Action类做单元测试,Struts 2对HttpServletRequest.HttpSession和ServletContext进行了封装,构造了 ...
- <s:if>标签与ActionContext.getContext().getSession()
今天在做<s:if>标签中的属性值从 ActionContext.getContext().getSession().put("WW_TRANS_I18N_LOCALE" ...
- 大约ActionContext.getContext()使用体验
这是我在另一个人的博客看了,原来博客的时间长一点.我把它简化了一下,运营商,以方便它看起来. 为了避免与Servlet API耦合在一起,方便Action类做单元測试,Struts 2对HttpSer ...
- ActionContext.getContext()用法
为了避免与Servlet API耦合在一起,方便Action类做单元测试,Struts 2对HttpServletRequest.HttpSession和ServletContext进行了封装,构造了 ...
- ValueStack与ContentMap (ActionContext.getContext().getValueStack().set())
在方法 <action name="zilei" class="dtreeAction" method="zilei"> & ...
随机推荐
- HDU 3398 String
题目大意:一个长为n的01字符串,使前缀任意0的数量不大于1的数量,求方案数…… 题解:高一模拟赛时做过,是卡特兰数的几何意义,将字符串变为矩阵寻路,不可越过对角线,那么就是卡特兰数了,C(n+m, ...
- Linux BFS简介
1. 什么是BFS 这里的BFS可不是广度优先算法,本文介绍的BFS是Linux的一个非Linux mainline调度算法.根据作者的描述BFS能够极大的提高低端设备(这里的低端设备的定义为:CPU ...
- android 中文 api (71) —— BluetoothServerSocket[蓝牙]
前言 本章内容是 android.bluetooth.BluetoothServerSocket,为Android蓝牙部分的章节翻译.服务器通讯套接字,与TCP ServerSocket类似.版本为 ...
- WPF与Winform的选择
最近公司计划对ERP系统全面升级,现有的ERP是简单的bs架构系统打算改版成cs.平时如自己写一些工具,小应用都是用winform就足够.但是界面总是很难看,据了解WPF在这一方面会强一些.因为之前对 ...
- java线程之停止线程
在Java中有以下3种方法可以终止一个正在运行的线程: 1.使用退出标志,是线程正常退出,也就是run方法完成后线程终止. 2.使用stop方法强制终止线程,但不推荐使用 ...
- C#中文件管理的运用(Twelfth Day)
又到了总结的时间了,今天在云和学院学习了文件管理的一些运用及复习昨天学的里氏转换.今天我就总结下昨天遗留下的问题以及今天所学的知识. 昨天遗留的问题 里氏转换(父类转子类) 例:在这里定义父类Peop ...
- BlogUI的使用
- Objects
Obeject Object Object representation and value representation Subobjects Polyomrphic objecets Alignm ...
- MySql 小问题集合
- 使用MySql通过SpringFramework来自动建表, 服务器用的是Tomcat, 在server.xml和context.xml中均正确配置了jdbc datasource. 编译通过, ...
- [LeetCode]题解(python):023-Merge k Sorted Lists
题目来源: https://leetcode.com/problems/merge-k-sorted-lists/ 题意分析: 给定k个有序的链表,将这些链表整合成一个新的有序链表. 题目思路: 前面 ...