Struts2是WebWork的升级版,采用的是WebWork的核心,所以,构建在WebWork基础之上的Struts2是一个运行稳定、性能优异、设计成熟的WEB框架。
一、主要的包和类
Struts2框架的正常运行,除了占核心地位的XWork的支持以外,Struts2本身也提供了许多类,这些类被分门别类组织到不同的包中。从源代码中发现,基本上每一个Struts2类都访问了WebWork提供的功能,但Struts2的核心功能比如将请求委托给哪个Action处理都是由XWork完成的,Struts2只是在WebWork的基础上做了适当的简化、加强和封装,并少量保留Struts1中的习惯。
对各包的简要说明:
包名 | 说明 |
org.apache.struts2. components | 该包封装视图组件,Struts2在视图组件上有了很大加强,不仅增加了组件的属性个数,更新增了几个非常有用的组件,如updownselect、doubleselect、datetimepicker、token、tree等。 另外,Struts2可视化视图组件开始支持主题(theme),缺省情况下,使用自带的缺省主题,如果要自定义页面效果,需要将组件的theme属性设置为simple。 |
org.apache.struts2. config | 该包定义与配置相关的接口和类。实际上,工程中的xml和properties文件的读取和解析都是由WebWork完成的,Struts只做了少量的工作。 |
org.apache.struts2.dispatcher | Struts2的核心包,最重要的类都放在该包中。 |
org.apache.struts2.impl | 该包只定义了3个类,他们是StrutsActionProxy、StrutsActionProxyFactory、StrutsObjectFactory,这三个类都是对XWork的扩展。 |
org.apache.struts2.interceptor | 定义内置的截拦器。 |
org.apache.struts2.util | 实用包。 |
org.apache.struts2.validators | 只定义了一个类:DWRValidator。 |
org.apache.struts2.views | 提供freemarker、jsp、velocity等不同类型的页面呈现。 |
一些重要类的说明:
类名 | 说明 |
org.apache.struts2.dispatcher. Dispatcher | 该类有两个作用: 1、初始化 2、调用指定的Action的execute()方法。 |
org.apache.struts2.dispatcher. FilterDispatcher | 该类有四个作用: 1、执行Action 2、清理ActionContext,避免内存泄漏 3、处理静态内容(Serving static content) 4、为请求启动xwork’s的截拦器链。 |
com.opensymphony.xwork2. ActionProxy | Action的代理接口。 |
com.opensymphony.xwork2. ctionProxyFactory | 生产ActionProxy的工厂。 |
com.opensymphony.xwork2.ActionInvocation | 负责调用Action和截拦器。 |
com.opensymphony.xwork2.config.providers. XmlConfigurationProvider | 负责Struts2的配置文件的解析。 |
二、Struts 2的核心控制器是FilterDispatcher
Struts2的启动是通过FilterDispatcher过滤器实现的,有3个重要的方法doFilter()、Init()、destroy()。
(1)过滤器在web.xml文件中的配置:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<url-pattern>/*</url-pattern>是用来过滤你想要过滤的请求,如果过滤所有请求,用*表示。
(2)StrutsStatics接口
public class FilterDispatcher implements StrutsStatics, Filter{}
FilterDispatcher除了实现Filter接口以外,还实现了StrutsStatics接口,StrutsStatics并没有实现业务逻辑,只是对常用的接口进行了重新封装,如:HttpServletRequest、HttpServletResponse等
源码如下:
public interface StrutsStatics {
/**
* Constant for the HTTP request object.
*/
public static final String HTTP_REQUEST = "com.opensymphony.xwork2.dispatcher.HttpServletRequest";
/**
* Constant for the HTTP response object.
*/
public static final String HTTP_RESPONSE = "com.opensymphony.xwork2.dispatcher.HttpServletResponse";
/**
* Constant for an HTTP {@link javax.servlet.RequestDispatcher request dispatcher}.
*/
public static final String SERVLET_DISPATCHER = "com.opensymphony.xwork2.dispatcher.ServletDispatcher";
/**
* Constant for the {@link javax.servlet.ServletContext servlet context} object.
*/
public static final String SERVLET_CONTEXT = "com.opensymphony.xwork2.dispatcher.ServletContext";
/**
* Constant for the JSP {@link javax.servlet.jsp.PageContext page context}.
*/
public static final String PAGE_CONTEXT = "com.opensymphony.xwork2.dispatcher.PageContext";
/** Constant for the PortletContext object */
public static final String STRUTS_PORTLET_CONTEXT = "struts.portlet.context";
/**
* Set as an attribute in the request to let other parts of the framework know that the invocation is happening inside an
* action tag
*/
public static final String STRUTS_ACTION_TAG_INVOCATION= "struts.actiontag.invocation";
}