美文网首页
struts2获取 servletAPI的三种方法

struts2获取 servletAPI的三种方法

作者: 凯凯_ | 来源:发表于2018-08-22 10:43 被阅读12次

1. 完全解耦合的方式

* 如果使用该种方式,Struts2框架中提供了一个类,ActionContext类,该类中提供一些方法,通过方     法获取Servlet的API

    * 一些常用的方法如下:

    1.getParameters() -- 获取请求参数,相当于request.getParameterMap();

     2.getSession() -- 获取的代表session域的Map集合,就相当于操作session域。

    3.getApplication() -- 获取代表application域的Map集合

大致使用方法

2.使用接口注入的方式

    * Struts2框架中提供了一些接口,编写的Action类可以是去实现这些接口,然后实现这些接口中的方法,这些方法都是把一些Servlet的常用对象通过参数的方式传递进来。

    * 常用的接口如下:

        * ServletRequestAware      -- 注入request

        * ServletContextAware      -- 注入ServletContext

        * ServletResponseAware      -- 注入response.

3.使用ServletActionContext中静态方法直接访问Servlet的API

    * Struts2框架提供了一个类,ServletActionContext,该类中提供了一些静态的方法

    * 具体的方法如下

        * getPageContext();

        * getRequest()

        * getResponse();

        * getServletContext();

相关文章

网友评论

      本文标题:struts2获取 servletAPI的三种方法

      本文链接:https://www.haomeiwen.com/subject/poeeiftx.html