这个也是我在另外一位仁兄的博客中看到的,原博客的有点长,我把它精简了一下,算看起来比較方便吧。

为了避免与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()的使用方法心得的更多相关文章

  1. 【转载】关于ActionContext.getContext().getParameters()获值问题

    ActionContext.getContext().getParameters():一个学员问题的解答 2012-11-12 15:12:05|  分类: 默认分类 |  标签:struts2   ...

  2. ServletActionContext.getRequest().getSession() 和 ActionContext.getContext().getSession()

    ActionContext.getContext().getSession(); 这个方法获取的session是struts封装过的一个Map类型的session,只能调用put()方法缓存数据. S ...

  3. 关于ActionContext.getContext()的用法心得

    转: 为了避免与Servlet API耦合在一起,方便Action类做单元测试,Struts 2对HttpServletRequest.HttpSession和ServletContext进行了封装, ...

  4. ActionContext.getContext().getSession()

    ActionContext.getContext().getSession() 获取的是session,然后用put存入相应的值,只要在session有效状态下,这个值一直可用 ActionConte ...

  5. 关于ActionContext.getContext()的用法

    为了避免与Servlet API耦合在一起,方便Action类做单元测试,Struts 2对HttpServletRequest.HttpSession和ServletContext进行了封装,构造了 ...

  6. <s:if>标签与ActionContext.getContext().getSession()

    今天在做<s:if>标签中的属性值从 ActionContext.getContext().getSession().put("WW_TRANS_I18N_LOCALE" ...

  7. 大约ActionContext.getContext()使用体验

    这是我在另一个人的博客看了,原来博客的时间长一点.我把它简化了一下,运营商,以方便它看起来. 为了避免与Servlet API耦合在一起,方便Action类做单元測试,Struts 2对HttpSer ...

  8. ActionContext.getContext()用法

    为了避免与Servlet API耦合在一起,方便Action类做单元测试,Struts 2对HttpServletRequest.HttpSession和ServletContext进行了封装,构造了 ...

  9. ValueStack与ContentMap (ActionContext.getContext().getValueStack().set())

    在方法 <action name="zilei" class="dtreeAction" method="zilei">   & ...

随机推荐

  1. BZOJ 2463 谁能赢呢? (博弈论)

    题解:简单博弈论 #include <cstdio> int main(){ int n; while(scanf("%d",&n),n!=0) if (n&a ...

  2. Oracle 存储过程DEMO

    );   cursor isbns is select ISBN,id from librarys;   bn isbns%rowtype;   begin  for bn in isbns  loo ...

  3. Poj 2232 Moo Volume(排序)

    题目链接:http://poj.org/problem?id=2231 思路分析:先排序,再推导计算公式. 代码如下: #include <iostream> #include <a ...

  4. c 整数的逆序输出 输入3,2就算 2+22+222的结果

    #include<stdio.h> #include<math.h> //整数逆序输出 void nixu() { int num,i; i = ; scanf("% ...

  5. Python学习之路——字符处理(二)

    一.set集合: set是一个无序且不重复的元素集合 建立一个集合: x = set([1, 'tom', 2, 3, 4]) print(type(x)) print(x) 以上实例运行后反回结果结 ...

  6. 深入浅出—JAVA(4)

    4.方法操作实例变量

  7. SQL Identity自增列清零方法

    1.使用DBCC控制台命令: dbcc checkident(表名,RESEED,0) 2.truncate table 也可将当前标识值清零 但当有外键等约束时,无法truncate表 可以先禁用外 ...

  8. java学习之内省

    反射加内省解决耦合问题 package com.gh.introspector; /** * JavaBean * @author ganhang * */ public class Dog { pr ...

  9. poj 1056 IMMEDIATE DECODABILITY(KMP)

    题目链接:http://poj.org/problem?id=1056 思路分析:检测某字符串是否为另一字符串的前缀,数据很弱,可以使用暴力解法.这里为了练习KMP算法使用了KMP算法. 代码如下: ...

  10. ARPU_百度百科

    ARPU_百度百科 ARPU