struts2与Servlet API解耦(Servlet相关对象访问)

在程序里面,讲究“高内聚,低耦合”。Struts2采用MVC架构,使用Action来处理http请求并进行响应,而不使用Servlet。

但是,我们知道,Servlet的含义就是“服务器端的小应用程序”,它包含了HttpServletRequest、HttpServletResponse、HttpSession和ServletContext等对象。当我们使用Struts2的Action处理Http请求时,虽然不用写Servlet,但是需要获取到Servlet相关的一些对象。这就是本节中要讲的内容。


以Servlet API解耦的访问方式,是为了降低 与Servlet API 的耦合。Struts2 构造 Map 对象来替代Servlet对象(包括 HttpServletRequest, HttpSession、 ServletContext), 在 Action中可直接使用Servlet对象对应的Map对象来存取数据。


本节主要介绍两种方式来访问Servlet相关的对象:(1)使用ServletActionContext类直接获取和(2)实现XXXAware接口间接获取Servetl相关的对象


1、ServletActionContext类访问

1.1、ServletActionContext介绍

ActionContext是Action执行上下文环境,每个上下文基本上都是一个对象容器,包含了动作执行所需的对象(session、参数、locale等)。ActionContext是thread local的,存放在每个线程中的值唯一,线程安全。

ServletActionContext直接继承ActionContext,提供了直接与Servlet相关对象访问的功能,可取得的对象有:

(1)javax.servlet.http.HttpServletRequest : HttpServlet请求对象

(2)javax.servlet.http.HttpServletResponse : HttpServlet响应对象

(3)javax.servlet.ServletContext : Servlet上下文信息

(4)javax.servlet.ServletConfig : Servlet配置对象

(5)javax.servlet.jsp.PageContext : Http页面上下文

这些对象基本上都是通过静态方法来获得。

wKioL1dpeUujHgr4AABzRQq6260927.png


1.2、ActionContext与ServletActionContext比较

ActionContext主要负责值的操作;

ServletActionContext主要负责获取Servlet对象。

原则是:优先使用ActionContext,只有ActionContext不能满足功能要求时才使用ServletActionContext,尽量使Action与Web无关,利于Action的测试和复用

需要注意:使用ActionContext时,不要在Action的构造函数里使用ActionContext.getContext()


1.3、使用思路

操作这些对象可从小到大


PageContext----Request----Session-----ServletContext(Application)

1
2
3
4
5
HttpServletRequest request = ServletActionContext.getRequest();  
HttpServletResponse response = ServletActionContext.getResponse();  
ServletContext servletContext = ServletActionContext.getServletContext();  
PageContext pageContext = ServletActionContext.getPageContext();  
HttpSession session = ServletActionContext.getRequest().getSession();


1.4、阅读文档的知识

ActionContext类

全名:com.opensymphony.xwork2.ActionContext

(1)ActionContext是Action运行的上下文环境。

The ActionContext is the context in which an Action is executed. 

(2)context(上下文环境)本质上来说,有些java培训里面对这块讲解的知识是错误的,这个其实就是一系列对象的容器,它为Action的执行提供了运行环境。

Each context is basically a container of objects an action needs for execution like the session, parameters, locale, etc. 

(3)ActionContext类属于thread local,它有线程安全的特性。

The ActionContext is thread local which means that values stored in the ActionContext are unique per thread. The benefit of this is you don't need to worry about a user specific action context, you just get it: 

ActionContext context = ActionContext.getContext();

Finally, because of the thread local usage you don't need to worry about making your actions thread safe.


ServletActionContext类

全名:org.apache.struts2.ServletActionContext

(1)这是一个为Action提供特定web信息的context类

Web-specific context information for actions. 

(2)ServletActionContext类的父类是ActionContext类。ActionContext类主要用于获取Action的name和value。

This class subclasses ActionContext which provides access to things like the action name, value stack, etc. 

(3)ServletActionContext类添加了获取web objects的方法。

This class adds access to web objects like servlet parameters, request attributes and things like the HTTP session.




2、实现XXXAware接口访问


实现Aware接口利用了依赖注入的方式能够让应用程序有更好的重用性,基本上java培训机构里面都会讲到这些内容。

Struts2提供了4种分别用来访问ServletContext、HttpServletRequest、HttpServletResponse、HttpSession对象的接口


org.apache.struts2.util.ServletContextAware;

org.apache.struts2.interceptor.ServletRequestAware;

org.apache.struts2.interceptor.ServletResponseAware;

org.apache.struts2.interceptor.SessionAware;


ServletContextAware接口源码:

1
2
3
4
5
6
7
8
9
10
11
12
package  org.apache.struts2.util;
 
import  javax.servlet.ServletContext;
 
 
/**
  * For components that have a dependence on the Servlet context.
  */
public  interface  ServletContextAware {
 
     public  void  setServletContext(ServletContext context);
}


ServletRequestAware接口源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package  org.apache.struts2.interceptor;
 
import  javax.servlet.http.HttpServletRequest;
 
 
/**
  * All Actions that want to have access to the servlet request object must implement this interface.<p>
  * <p/>
  * This interface is only relevant if the Action is used in a servlet environment. <p>
  * <p/>
  * Note that using this interface makes the Action tied to a servlet environment, so it should be
  * avoided if possible since things like unit testing will become more difficult.
  *
  */
public  interface  ServletRequestAware {
 
     /**
      * Sets the HTTP request object in implementing classes.
      *
      * @param request the HTTP request.
      */
     public  void  setServletRequest(HttpServletRequest request);
}


ServletResponseAware接口源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package  org.apache.struts2.interceptor;
 
import  javax.servlet.http.HttpServletResponse;
 
 
/**
  * All Actions that want to have access to the servlet response object must implement this interface.<p>
  * <p/>
  * This interface is only relevant if the Action is used in a servlet environment.<p>
  * <p/>
  * Note that using this interface makes the Action tied to a servlet environment, so it should be
  * avoided if possible since things like unit testing will become more difficult.
  *
  */
public  interface  ServletResponseAware {
 
     /**
      * Sets the HTTP response object in implementing classes.
      *
      * @param response the HTTP response.
      */
     public  void  setServletResponse(HttpServletResponse response);
}


SessionAware接口源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package  org.apache.struts2.interceptor;
 
import  java.util.Map;
 
 
/**
  * Actions that want access to the user's HTTP session attributes should implement this interface.<p>
  * <p/>
  * This will give them access to a Map where they can put objects that can be made available
  * to subsequent requests.<p/>
  * <p/>
  * Typical uses may be cached user data such as name, or a shopping cart.
  *
  */
public  interface  SessionAware {
 
     /**
      * Sets the Map of session attributes in the implementing class.
      *
      * @param session a Map of HTTP session attribute name/value pairs.
      */
     public  void  setSession(Map<String,Object> session);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值