XWork Features-

来源:互联网 发布:一个人做淘宝 心酸 编辑:程序博客网 时间:2024/06/04 23:25

“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 Actioninterface. You define action classes by implementing an Actioninterface, then XWork will setup and execute your actions. XWork ismost widely known from the web MVC framework called Webwork. However,XWork can be used by itself, so its important to understand the XWorklayers and how actions are set up and executed. This section describesthe core layers within Xwork and provides a simple example of how toexecute 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 asingle method, execute(), that defines the entry point called by theframework. This method allows developers to define a unit of work thatwill be executed each time the Action is called.

~Action是响应请求、执行定义的操作、返回一个结果对象的类。其核心方法只有一个,就是execute(),这个方法是框架执行方法的入口。

ActionContext

The ActionContext provides access to the execution environment inthe form of named objects during an Action invocation. A newActionContext is created for each invocation allowing developers toaccess/modify these properties in a thread safe manner. TheActionContext makes a number of properties available that are typicallyset 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 thecore framework to a specific execution environment. For moreinformation, see ActionContext in Basics.

~~ActionContext类,可以使得Action在调用时,通过其获取到执行环境中变量的,并且是线程安全的方式!在WebWork2中,ActionContext的sessionmap变量中就包装了HttpSession对象的值,这样就可以在action调用的过程中获取到环境变量值。

Interceptors

In XWork, Interceptors are objects that dynamically intercept Actioninvocations. They provide the developer with the opportunity to definecode that can be executed before and/or after the execution of anaction. They also have the ability to prevent an action from executing.Interceptors provide developers a way to encapulate commonfunctionality in a re-usable form that can be applied to one or moreActions. See Interceptors for further details.

~~拦截器,before/after类型,没有环绕型的:) 拦截器可以组成链,使得一个Action调用经过顺序的拦截器

Stacks

To handle the case where developers want to apply more than a singleInterceptor to an Action, Stacks have been introduced. Stacks are anordered list of Interceptors and/or other Stacks that get applied whenan Action is invoked. Stacks centralize the declaration of Interceptorsand provide a convenient way to configure mutiple actions.

~~栈在xwork中,主要是用来描述多个拦截器顺序执行的一个概念。

Results

Results are string constants that Actions return to indicate thestatus of an Action execution. A standard set of Results are defined bydefault: error, input, login, none and success. Developers are, ofcourse, free to create their own Results to indicate more applicationspecific cases.

 

~~~定义了Action执行后的返回。即命令模式的出口

Result Types

Result Types are classes that determine what happens after an Actionexecutes and a Result is returned. Developers are free to create theirown Result Types according to the needs of their application orenvironment. In WebWork 2 for example, Servlet and Velocity ResultTypes have been created to handle rendering views in web applications.

Packages

Packages are a way to group Actions, Results, Result Types,Interceptors and Stacks into a logical unit that shares a commonconfiguration. Packages are similiar to objects in that they can beextended and have individual parts overridden by "sub" packages.

ValueStack

The ValueStack is a stack implementation built on top of an OGNLcore. The OGNL expression language can be used to traverse the stackand retrieve the desired object. The OGNL expression language providesa number of additional features including: automatic type conversion,method invocation and object comparisons. For more information, see theOGNL Website.

 

~stack模式的变量堆栈。struts2与页面之间的变量赋值,其实就是这个特性。支持OGNL。

Components

XWork provides the ComponentManager interface (and a correspondingimplementation in the DefaultComponentManager class) to apply a designpattern known as Inversion of Control (or IoC for short). In anutshell, the IoC pattern allows a parent object (in this case XWork'sComponentManager instance) to control a client object (usually anaction, 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 storagebook = 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 parametersMap 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 infoActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy("","viewBook", context);// we have the action proxy instance, lets execute it and retrieve the actionString 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>
 

 

最后来一张总图: