`
sw1982
  • 浏览: 511417 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

XWork Features-

阅读更多

“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(),这个方法是框架执行方法的入口。

ActionContext

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调用的过程中获取到环境变量值。

Interceptors

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调用经过顺序的拦截器

Stacks

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

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

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

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.

ValueStack

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。

Components

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>
 

 

最后来一张总图:


 

  • 大小: 140.8 KB
分享到:
评论

相关推荐

    xwork各版本的jar包

    xwork-1.1.1.jar, xwork-1.1.jar, xwork-1.2.1.jar, xwork-1.2.3.jar, xwork-2.0.0.jar, xwork-2.0.1.jar, xwork-2.0.3.jar, xwork-2.0.4.jar, xwork-2.0.5.jar, xwork-2.0.6.jar, xwork-2.0.7.jar, xwork-2.1.0.jar...

    xwork-2.1.5-all.rar_xwork_xwork-2.1.5_xwork-2.1.5.jar_xwork-core

    xwork-2.1.5.jar包含了整个框架的功能,而xwork-core-2.1.6.jar则是核心模块,包含Action、Interceptor等关键组件。 7. **与Struts2的关系**: XWork与Struts2紧密相关,Struts2是基于XWork构建的一个MVC框架,它...

    xwork-core-2.3.4.1-sources.jar.zip 源代码

    《深入解析xwork-core-2.3.4.1源代码》 xwork-core是Struts2框架的核心组件,它提供了Action、Interceptor、ValueStack等关键功能,是理解Struts2工作原理的重要部分。源代码的分析能让我们更深入地了解这个框架的...

    xwork-core-2.3.37.jar

    用于解决struts2升级至2.3.37时 出现java.lang.NoSuchMethodError: com.opensymphony.xwork2.ActionContext.put(Ljava/lang/异常时,更换的xwork-core-2.3.37.jar

    xwork-core-2.3.15.1.jar

    xwork-core-2.3.15.1.jar

    xwork--- jar包

    `xwork-2.1.5`是XWork的一个特定版本,可能包含了一些新特性、性能优化和已知问题的修复。随着Struts2的更新,XWork也会相应升级以保持兼容性并提供更完善的功能。 总之,XWork jar包是构建基于Struts2的Web应用...

    xwork-core-2.2.1.1.jar

    xwork-core-2.2.1.1.jar

    JavaEE源代码 xwork-2.0.4

    JavaEE源代码 xwork-2.0.4JavaEE源代码 xwork-2.0.4JavaEE源代码 xwork-2.0.4JavaEE源代码 xwork-2.0.4JavaEE源代码 xwork-2.0.4JavaEE源代码 xwork-2.0.4JavaEE源代码 xwork-2.0.4JavaEE源代码 xwork-2.0.4JavaEE源...

    xwork-core-2.3.16.1

    xwork-core-2.3.16.1.jar,欢迎大家下载。

    xwork-core-2.2.1-sources

    《深入解析xwork-core-2.2.1-sources》 xwork-core是Struts2框架的核心组件之一,它在Web应用开发中扮演着至关重要的角色。本文将深入探讨xwork-core-2.2.1-sources中的核心概念、功能以及源代码分析,帮助开发者更...

    xwork-assembly-2.1.6-src

    《深入剖析xwork-assembly-2.1.6-src:揭秘源码的奥秘》 在软件开发领域,源码是理解系统运作机制的关键。"xwork-assembly-2.1.6-src"是一个开放源码的项目,它为我们提供了深入了解xwork框架核心功能的机会。这个...

    xwork-core-2.3.34.jar(内含StringUtils.class)

    xwork-core-2.1.6.jar这个版本才内含StringUtils.class这个工具类,我已经将这个类添加到xwork-core-2.3.34.jar内了。

    xwork-core-2.2.1.2修正包

    修正Eclipse中启动JBoss时,提示:Could not create JarEntryRevision for……。 Struts2 2.2.1.2中xwork-core修正包xwork-core-2.2.1.2-SNAPSHOT。

    xwork-2.1.2.rar_xwork-2.1.2_xwork-core_xwork-core-2.1.2_xwork2.1

    这个压缩包"**xwork-2.1.2.rar**"包含了XWork框架的版本2.1.2,它是Struts2的重要组成部分。 **XWork核心组件详解:** 1. **动作调度(ActionInvocation)**:XWork通过ActionInvocation负责调用和管理Action的生命...

    xwork-validator-1.0.3.dtd

    在没有联网的请求本地添加验证xml文件,添加验证XML文件的DTD

    xwork-2.0.6-src

    《深入解析xwork-2.0.6源码:打造高效Struts2应用》 xwork-2.0.6-src是Struts2框架的核心组件之一,它包含了xwork库的源代码,对于理解Struts2的工作原理以及进行二次开发具有重要的参考价值。本文将深入探讨xwork-...

    xwork-core源码

    《深入解析xwork-core源码:为Struts学习铺路》 在Java Web开发领域,Struts框架无疑占据着重要的地位。而xwork-core作为Struts2的核心组件,它的源码解析对于理解Struts的工作机制至关重要。本文将深入探讨xwork-...

    xwork-core-2.2.1.1.zip

    《XWork核心库详解——基于xwork-core-2.2.1.1.zip》 XWork,作为Struts2框架的重要组成部分,为Java Web开发提供了强大的动作调度和业务逻辑处理能力。本文将深入探讨xwork-core-2.2.1.1.zip这个压缩包中的核心...

    xwork-core-2.3.31

    xwork-core-2.3.31

    xwork-core-source-2.3.1.2源码

    《深入剖析xwork-core-source-2.3.1.2:构建Struts2核心基石》 xwork-core-source-2.3.1.2是Struts2框架的核心库,其源代码提供了对Struts2底层机制的深入了解。Struts2作为一款广泛应用的Java Web MVC框架,xwork-...

Global site tag (gtag.js) - Google Analytics