“XWork 2 is a generic command pattern framework. It forms the core of Struts 2 .”
~xwork2 是struts2 的核心!!!是基于命令模式的一个框架。其实要看懂xwork基本就不需要看struts2了、
个人理解的xwork,核心就是代理模式+AOP的拦截器, 命令模式实现,线程变量类!
Xwork is a command pattern framework centralized around an Action interface. You define action classes by implementing an Action interface, then XWork will setup and execute your actions. XWork is most widely known from the web MVC framework called Webwork. However, XWork can be used by itself, so its important to understand the XWork layers and how actions are set up and executed. This section describes the core layers within Xwork and provides a simple example of how to execute actions.
~~Xwork是以Action接口为中心的命令模式实现框架。定义一个Action的实现类,然后Xwork将装配并执行它。其核心接口如下:
- Action Interface
- ActionProxy interface
- ActionInvocation interface
- ActionContext
- A simple example
Actions
Actions are classes that get invoked in response to a request, execute some code and return a Result. Actions implement at a minimum a single method, execute(), that defines the entry point called by the framework. This method allows developers to define a unit of work that will be executed each time the Action is called.
~Action是响应请求、执行定义的操作、返回一个结果对象的类。其核心方法只有一个,就是execute(),这个方法是框架执行方法的入口。
The ActionContext provides access to the execution environment in the form of named objects during an Action invocation. A new ActionContext is created for each invocation allowing developers to access/modify these properties in a thread safe manner. The ActionContext makes a number of properties available that are typically set to appropriate values by the framework. In WebWork 2 for example, the ActionContext session map wraps an underlying HttpSession object. This allows access to environment specific properties without tying the core framework to a specific execution environment. For more information, see ActionContext in Basics.
~~ActionContext类,可以使得Action在调用时,通过其获取到执行环境中变量的,并且是线程安全的方式!在WebWork2中,ActionContext的sessionmap变量中就包装了HttpSession对象的值,这样就可以在action调用的过程中获取到环境变量值。
In XWork, Interceptors are objects that dynamically intercept Action invocations. They provide the developer with the opportunity to define code that can be executed before and/or after the execution of an action. They also have the ability to prevent an action from executing. Interceptors provide developers a way to encapulate common functionality in a re-usable form that can be applied to one or more Actions. See Interceptors for further details.
~~ 拦截器,before/after类型,没有环绕型的:) 拦截器可以组成链,使得一个Action调用经过顺序的拦截器
To handle the case where developers want to apply more than a single Interceptor to an Action, Stacks have been introduced. Stacks are an ordered list of Interceptors and/or other Stacks that get applied when an Action is invoked. Stacks centralize the declaration of Interceptors and provide a convenient way to configure mutiple actions.
~~栈在xwork中,主要是用来描述多个拦截器顺序执行的一个概念。
Results are string constants that Actions return to indicate the status of an Action execution. A standard set of Results are defined by default: error, input, login, none and success. Developers are, of course, free to create their own Results to indicate more application specific cases.
~~~定义了Action执行后的返回。即命令模式的出口
Result Types are classes that determine what happens after an Action executes and a Result is returned. Developers are free to create their own Result Types according to the needs of their application or environment. In WebWork 2 for example, Servlet and Velocity Result Types have been created to handle rendering views in web applications.
Packages are a way to group Actions, Results, Result Types, Interceptors and Stacks into a logical unit that shares a common configuration. Packages are similiar to objects in that they can be extended and have individual parts overridden by "sub" packages.
The ValueStack is a stack implementation built on top of an OGNL core. The OGNL expression language can be used to traverse the stack and retrieve the desired object. The OGNL expression language provides a number of additional features including: automatic type conversion, method invocation and object comparisons. For more information, see the OGNL Website .
~stack模式的变量堆栈。struts2与页面之间的变量赋值,其实就是这个特性。支持OGNL。
XWork provides the ComponentManager interface (and a corresponding implementation in the DefaultComponentManager class) to apply a design pattern known as Inversion of Control (or IoC for short). In a nutshell, the IoC pattern allows a parent object (in this case XWork's ComponentManager instance) to control a client object (usually an action, but it could be any object that implements the appropriate enabler ). See Components for further details.
~~对象管理组件,不过IOC通常推荐spring来代替,这里值得一看的还是ActionProxyFactory这个工厂类。
下面就是一组代码了,通过定义一个Action,创建并执行的过程,就可以清楚的了解到整个框架的运作过程:
1.定义action
public class ViewBookAction implements Action{
Book book;
String id;
public String execute() throws Exception{
// lets pretend we have a data access object that will return a book from storage
book = bookDAO.findById(id, Book.class);
if(book != null) return "success";
return "notFound";
}
public Book getBook(){ return this.book; }
public setId(String id){this.id = id; }
}
2.定义环境变量(ActionContext.),工厂方法构建ActionProxy(Action的代理),然后代理获取Action实例,执行。
// obtain inputs from the caller. For this example, we can just define some dummy params.
Map paramMap = new HashMap();
paramMap.put("id", "0123456789");
// set the ActionContext parameters
Map context = new HashMap();
context.put(ActionContext.PARAMETERS, paramMap);
// create an action proxy with no namespace, action alias (defined in xwork.xml), and a map of the context info
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy("","viewBook", context);
// we have the action proxy instance, lets execute it and retrieve the action
String result = proxy.execute();
if ("success".equals(result)){
ViewBookAction action = (ViewBookAction) proxy.getAction();
// return info back to caller or just print to screen for this example
System.out.println(action.getBook().getTitle());
} else if("notFound".equals(result){
// forward to another inventory source
} else {
throw new RuntimeException("Im lazy");
}
3.简单的配置文件。是不是很熟悉呢。。struts2基本就是抄这里了
<xwork> <include file="xwork-default.xml"/> <package name="default" extends="xwork-default"> <action name="viewBook" class="com.opensymphony.xwork.example.ViewBookAction"/> </package> </xwork>
最后来一张总图: