`

Struts2通过使用ActionContext类获取request和response对象

 
阅读更多

使用ActionContext类
这种方法比较简单,我们可以通过org.apache.struts2.ActionContext类的get方法获得相应的对象。代码如下:

HttpServletResponse response = (HttpServletResponse)ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
HttpServletRequest  request = (HttpServletRequest)ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);

 

 

使用ServletActionContext类
Struts2为我们提供了一种最简单的方法获得HttpServletResponse及其他对象。
这就是org.apache.struts2.ServletActionContext类。
我们可以直接使用ServletActionContext类的getRequest、getResponse方法来获得
HttpServletRequest、HttpServletResponse对象。

 

代码如下:

  HttpServletResponse response = ServletActionContext.getResponse();
  response.getWriter().write("hello world");

 

分享到:
评论

相关推荐

    基于struts2注册登录ActionContext.zip

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

    struts2中的ActionContext与ognl

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

    struts2OGNL表达式ActionContext及valuesStack.pdf

    struts2OGNL表达式ActionContext及valuesStack.pdf

    Struts2中Servlet的配置

    2、使用struts2提供的ActionContext类直接获取Servlet API。 在同一个项目中,如果既用到servlet又用了struts框架,运行项目时可能无法正常使用servlet,解决办法如下: 该类是定义了一个过滤器,当既用到struts2...

    ActionContext介绍(在Struts2中)

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

    struts2_OGNL表达式ActionContext及valuesStack

    struts2 OGNL,struts2 表达式语言,Struts2 中OGNL表达式的用法,Struts2 #,表达式语言的好处,Struts2 $,struts2 井号,星号,百分号

    Struts2_TypeConvertion

    这种方式主要是利用了com.opensymphony.xwork2.ActionContext类以及org.apache.struts2.ServletActionContext类,具体的方法如下所示。 获得request对象: A . HttpServletRequest request = ServletActionContext....

    STRUTS2获得作用域、参数响应对象及三种符号说明

    RequestMap,SessionMap,ApplicationMap, AttributeMap. struts提供两种方式访问session,request ,response,ActionContext 使用。 获得作用域及参数响应对象及三种符号说明

    Struts2 in action中文版

    13.3.3 连接UI标签和对象 287 13.4 高级UI标签的使用 288 13.4.1 覆盖既有模板 288 13.4.2 编写自定义模板 288 13.4.3 编写自定义主题 289 13.5 小结 289 第14章 从经典Struts迁移 290 14.1 翻译经典Struts的知识 ...

    struts2流程与流程图

    一个请求在Struts 2框架中的处理大概分为以下几个步骤。...Struts 2的核心控制器是FilterDispatcher,有3个重要的方法:destroy()、doFilter()和Init(),可以在Struts 2的下载文件夹中找到源代码,如代码1所示。

    Struts2实战总结

    3:在action类中取得request和session对象的方法 Map session = ActionContext.getContext().getSession(); HttpServletRequest request = ServletActionContext.getRequest (); 设置它们的值的方法

    Struts2整合SiteMesh技巧

    要使用Sitemesh需要包含Freemark,Sitemesh和Sitemesh插件库文件。 配置过滤器 如果需要使用Freemark模板文件作为装饰器文件,需要在web.xml文件中添加如下配置: xml 代码 <filter> <filter-name>struts-clean...

    Struts2 学习笔记

    01 Struts2-Action 5 一、 Struts作用: 5 二、 搭建Struts2的运行环境: 5 三、 Namespace 6 四、 标签 6 五、 Action 6 六、 路径问题的说明 8 七、 Action的动态调用方法 8 八、 Action通配符(wildcard)的配置 9 ...

    struts2验证码完整实例

    (参照别人的代码) 部署: 1、将整个源码引入到MyEclipse当中. 2、部署到Tomcat下面,启动服务器,直接 敲...2、当你登录时候,提交的输入框中的验证码和session中存放的验证码比较,如果一样,则通过,不一样,则失败

    struts学习笔记(3)

    向页面传值可以使用struts2中特有的两个类的对象 com.opensymphony.xwork2.ActionContext和com.opensymphony.xwork2.util.ValueStack 1)ValueStack和ActionContext的作用: 当客户端向action发送请求并且最后跳转...

    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...

    Struts2帮助```````

    01 Struts2-Action 5 一、 Struts作用: 5 二、 搭建Struts2的运行环境: 5 三、 Namespace 6 四、 标签 6 五、 Action 6 六、 路径问题的说明 8 七、 Action的动态调用方法 8 八、 Action通配符(wildcard)的配置 9 ...

    OGNL表达式struts2标签

    2、支持类静态的方法调用和值访问,表达式的格式为@[类全名(包括包路)]@[方法名 | 值名],例如: @java.lang.String@format('foo %s', 'bar')或@tutorial.MyConstant@APP_NAME; 3、支持赋值操作和表达式串联,...

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

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

Global site tag (gtag.js) - Google Analytics