`
zhb0917
  • 浏览: 21562 次
社区版块
存档分类
最新评论

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

 
阅读更多
[转]http://blog.csdn.net/smcfy/article/details/5693481

为了避免与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);  
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>  
<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")%>

分享到:
评论

相关推荐

    Struts2_TypeConvertion

    B.ActionContext ct= ActionContext.getContext() HttpServletRequest request= (HttpServletRequest)ct.get(ServletActionContext. HTTP_REQUEST ); 获得session对象: 在Struts2中底层的session都被封装成了...

    JS客户端RSA加密,Java服务端解密

    ActionContext.getContext().put("exponent", new String(Hex.encodeHex(publicKey.getPublicExponent().toByteArray()))); // 页面里,Javascript对明文进行加密: 09 var modulus = $('#hid_modulus').val(); var...

    基于struts2注册登录ActionContext.zip

    struts2大量使用拦截器来处理请求,从而允许与业务逻辑控制器 与 servlet-api分离,避免了侵入性(所谓侵入性就是指的这个架构设计出来的部件对系统的影响范围,标签库几乎可以完全替代JSTL的标签库,并且 struts2.x...

    struts2中范围对象的操作

    ActionContext actionContext=ActionContext.getContext(); Object obj=actionContext.get("objName");----Object obj=request.getAttribute("objName"); actionContext.put("objName",objName);---request....

    Struts2实战总结

    1:在action中定义的变量,在jsp页面中显示用:变量名" /&gt; ...Map session = ActionContext.getContext().getSession(); HttpServletRequest request = ServletActionContext.getRequest (); 设置它们的值的方法

    struts2验证码完整实例

    通过ActionContext.getContext().getSession().put("random", randomNum.getRandomCode())将数字存放到session当中 2、当你登录时候,提交的输入框中的验证码和session中存放的验证码比较,如果一样,则通过,不一样,则...

    Struts2访问servlet分享

     ActionContext ctx = ActionContext.getContext();  ctx.getApplication().put(“app”, “应用范围”);//往ServletContext里放入app  ctx.getSession().put(“ses”, “session范围”);//往session里放入ses  ...

    struts2文件上传,与struts2-xml校验

    System.out.println("上下文路径="+ ActionContext.getContext()); System.out.println("aaa="+ServletActionContext.getActionContext(ServletActionContext.getRequest())); filename=new String[myFile....

    struts2OGNL表达式ActionContext及valuesStack.pdf

    struts2OGNL表达式ActionContext及valuesStack.pdf

    javaee三大框架整合宅急送项目lib包

    ActionContext.getContext().put("zhongZhuanInfoList", zhongZhuanInfoList); return "findInStoreTaskSUCCESS"; } // 业务方法 ----- 查询出库环节 的个人任务 public String findOutStoreTask...

    Struts2注解+JDBC+MySQL小项目应用

    Map, Object&gt; session = ActionContext.getContext().getSession(); session.put("userName", userName); session.put("passWord", passWord); // 2. taglib标识传递 resultMsg = "注册成功"; return ...

    struts2-junit-plugin-2.1.8.jar

    可解决java.lang.NoSuchMethodError: com.opensymphony.xwork2.ActionContext.get(Ljava/lang/Object;)Ljava/lang/Object; java.lang.ClassNotFoundException: com.opensymphony.xwork2.util.TextUtils struts2.1.8...

    xwork-core-2.3.37.jar

    用于解决struts2升级至2.3.37时 出现java.lang.NoSuchMethodError: com.opensymphony.xwork2.ActionContext.put(Ljava/lang/异常时,更换的xwork-core-2.3.37.jar

    Struts2 s2-061 Poc分析1

    简介分析性。2. 已实例类的法调 (OgnlContext 中的对象),不允许调静态法idea 中可以通过 ActionContext.actionContex

    ActionContext在struts2.0中的详细应用

    ActionContext(com.opensymphony.xwork.ActionContext)是Action执行时的上下文,上下文可以看作是一个容器(其实我们这里的容器就是一个Map而已),它存放放的是Action在执行时需要用到的对象

    使用Action访问ActionContext方式的网站计数器

    使用Action访问ActionContext方式的网站计数器,可直接运行

    指定IP地址才能访问API

    string ip = IpHelper.GetRealIP(); if (ip == ConfigurationManager.AppSettings["ApiUrl"]) { //HttpContext.Current.... actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.OK); }

    ActionContext介绍(在Struts2中)

    在Web应用程序开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话 (Session)的一些信息, 甚至需要直接对JavaServlet Http的请求(HttpServletRequest),响应...

    struts2中的ActionContext与ognl

    NULL 博文链接:https://lijiejava.iteye.com/blog/628636

    Flux应用可插拔容器Fluxible.zip

    特征:服务器可免费使用单体模式进行渲染无状态异步操作便于集成的高阶组件增强磁通流动 可在 Flux 内容上增加你的接口更新至 React 0.13示例代码:import Fluxible from 'fluxible'; import React from '...

Global site tag (gtag.js) - Google Analytics