转:

为了避免与Servlet API耦合在一起,方便Action类做单元测试,Struts 2对HttpServletRequest、HttpSession和ServletContext进行了封装,构造了三个Map对象来替代这三种对象,在Action中,直接使用HttpServletRequest、HttpSession和ServletContext对应的Map对象来保存和读取数据。

(一)通过ActionContext来获取request、session和application对象的LoginAction1

  1. ActionContext context = ActionContext.getContext();
  2. Map request = (Map)context.get("request");
  3. Map session = context.getSession();
  4. Map application = context.getApplication();
  5. request.put("greeting", "欢迎您来到程序员之家");//在请求中放置欢迎信息。
  6. session.put("user", user);//在session中保存user对象
  7. application.put("counter", count);

在JSP中读取

  1. <body><h3>${sessionScope.user.username},${requestScope.greeting}。<br>本站的访问量是:${applicationScope.counter}</h3>
  2. </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()的用法

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

  2. 关于ActionContext.getContext()的使用方法心得

    这个也是我在另外一位仁兄的博客中看到的,原博客的有点长,我把它精简了一下,算看起来比較方便吧. 为了避免与Servlet API耦合在一起,方便Action类做单元測试,Struts 2对HttpSe ...

  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().getParameters()获值问题

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

  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. ServletActionContext.getRequest().getSession() 和 ActionContext.getContext().getSession()

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

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

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

随机推荐

  1. PHPCMS系统使用的弹出窗口插件artDialog

    来源: http://aui.github.io/artDialog/doc/index.html  (官方) http://lab.seaning.com/ http://www.mb5u.com/ ...

  2. Android架构:用消息机制获取网络数据

    网络请求,不管是什么协议,是长连接还是短连接,总是一个异步的请求,过程包括:加请求参数->发起请求->接收响应->解析数据->获得业务数据. 最挫的做法是,业务代码包揽所有这些 ...

  3. OI总结(垃圾排版就忽略了吧)

    学OI一年了,到现在联赛所需要的知识已经基本学完了.现在,有必要回过头来,总结总结自己一年来学到的知识以及得到的经验教训. 基础 语言基础 C++的语言基础啥的就略了吧. 算法复杂度分析 O:复杂度的 ...

  4. Ext treelist 动态切换TreeStore

    chooseMenu: function(_this) { //var mycomp = top.Ext.getCmp("my_comp"); var menuTreeStore ...

  5. ffmpeg解码视频流

    //初始化.注册编解码器 avcodec_init(); av_register_all(); avformat_network_init(); //选取测试文件 char* FileName = & ...

  6. Python连接MySQL的准备工作

    首先要安装MySQL,64位的win7可以安装64或者32位的MySQL版本,安装之后,python需要一个工具才能连接MySQL,这个工具叫MySQL-python,去这里或者这里下载1.2.3版本 ...

  7. 利用php的register_shutdown_function来记录php的输出日志

    最近在做的一个项目..由于全是通过远程http请求来调用php的接口程序.. 接收到的参数和返回的内容对开发人员来说都是未知不可见的.. 虽然可以通过直接在脚本中模拟请求..但由于实际环境复杂的多.. ...

  8. HTTP Servlet 重要的几个方法

    HTTP Servlet继承了GencenServlet类    GencenServlet实现了两个接口··一个用于ServletConfig设置接口,一个为Servlet接口只要是(1) init ...

  9. css3 transition的各种ease效果

    http://www.w3school.com.cn/tiy/t.asp?f=css3_transition-timing-function2 linear 平均速度 ease 快启动,慢停止,物理原 ...

  10. 使用Axis2编写webservice客户端,服务端

    1.编写客户端 Axis2开发WebService客户端 的3种方式 [参考帖子] http://blog.csdn.net/wangjinwei6912/article/details/851259 ...