`
as619864232
  • 浏览: 324272 次
社区版块
存档分类
最新评论

Struts1 与 Struts2 的比较

阅读更多
Action classes |  Threading Model |  Servlet Dependency |  Testability |  Harvesting Input |  Expression Language |  Binding values into views |  Type Conversion |  Validation |  Control Of Action Execution
Struts 1 requires Action classes to extend an abstract base class. A common problem in Struts 1 is programming to abstract classes instead of interfaces. An Struts 2 Action may implement an Action interface, along with(连同) other interfaces to enable optional and custom services. Struts 2 provides a base ActionSupport class to implement commonly used interfaces. Albeit(尽管), the Action interface is not required. Any POJO object with a execute signature(签名) can be used as an Struts 2 Action object.
Struts 1 Actions are singletons(单例)and must be thread-safe since there will only be one instance of a class to handle all requests for that Action. The singleton(单例) strategy(策略) places restrictions(限制) on what can be done with Struts 1 Actions and requires extra(额外) care(小心) to develop. Action resources must be thread-safe or synchronized.

Struts 2 Action objects are instantiated for each request, so there are no thread-safety issues(问题). (In practice, servlet containers generate many throw-away(废物) objects per(每一) request, and one more object does not impose a performance penalty or impact garbage collection.)

Struts 1 Actions have dependencies(依赖性)  on the servlet API since the HttpServletRequest and HttpServletResponse is passed to the execute method when an Action is invoked. Struts 2 Actions are not coupled to a container. Most often the servlet contexts are represented(描述) as simple Maps, allowing Actions to be tested in isolation(隔离). Struts 2 Actions can still access the original(原始的) request and response, if required. However, other architectural elements reduce(减少) or eliminate(排除) the need to access the HttpServetRequest or HttpServletResponse directly.
A major(主要的) hurdle(障碍) to testing Struts 1 Actions is that the execute method exposes(暴露) the Servlet API. A third-party(第三方) extension(扩展), Struts TestCase, offers a set of)(一套) mock object(模拟对象) for Struts 1. Struts 2 Actions can be tested by instantiating the Action, setting properties, and invoking methods. Dependency Injection(依赖注入) support also makes testing simpler.
Struts 1 uses an ActionForm object to capture(捕获) input. Like Actions, all ActionForms must extend a base class. Since  other JavaBeans cannot be used as ActionForms, developers often create redundant(多余的) classes to capture(捕获) input. DynaBeans can used as an alternative(替换物) to creating conventional(常规的) ActionForm classes, but, here too, developers may be redescribing existing JavaBeans. 
Struts 2 uses Action properties as input properties, eliminating(消除) the need for a second input object. Input properties may be rich(丰富的) object types which may have their own properties. The Action properties can be accessed from the web page via(通过) the taglibs. Struts 2 also supports the ActionForm pattern, as well as POJO form objects and POJO Actions. Rich(丰富的) object types, including business(商业) or domain(领域) objects, can be used as input/output objects. The ModelDriven feature simplifies(简化) taglb references to POJO input objects. 
Struts 1 integrates(结合) with JSTL, so it uses the JSTL EL. The EL has basic object graph(图标) traversal(遍历), but relatively(相对的) weak(弱) collection and indexed property support. Struts 2 can use JSTL, but the framework also supports a more powerful and flexible expression language called "Object Graph Notation Language" (OGNL).
Struts 1 uses the standard(标准的) JSP mechanism(机制) for binding objects into the page context for access(存取). Struts 2 uses a "ValueStack" technology(技术) so that the taglibs can access values without coupling(耦合) your view to the object type it is rendering. The ValueStack strategy(策略) allows reuse of views across(穿过) a range of(一些) types which may have the same property name but different property types. 
Struts 1 ActionForm properties are usually all Strings. Struts 1 uses Commons-Beanutils for type conversion. Converters are per-class, and not configurable per instance. Struts 2 uses OGNL for type conversion. The framework includes converters for basic and common object types and primitives(基本).
Struts 1 supports manual(手册) validation(校验) via(通过) validate method on the ActionForm, or through an extension to the Commons Validator. Classes can have different validation contexts for the same class, but cannot chain to validations on sub-objects. Struts 2 supports manual validation via the validate method and the XWork Validation framework. The Xwork Validation Framework supports chaining validation into sub-properties using the validations defined for the properties class type and the validation context.
Struts 1 supports separate(单独的) Request Processors (lifecycles(生命周期)) for each module(模块), but all the Actions in the module must share the same lifecycle. Struts 2 supports creating different lifecycles on a per Action basis via Interceptor Stacks. Custom stacks can be created and used with different Actions, as needed.

 

 

 

 

以上为本人试练翻译,以下为转自前辈的中文翻译(http://ikingqu.iteye.com/blog/68597)

 

 

Action 类: 
• Struts1要求Action类继承一个抽象基类。Struts1的一个普遍问题是使用抽象类编程而不是接口。 
• Struts 2 Action类可以实现一个Action接口,也可实现其他接口,使可选和定制的服务成为可能。Struts2提供一个ActionSupport基类去 实现 常用的接口。Action接口不是必须的,任何有execute标识的POJO对象都可以用作Struts2的Action对象。 

线程模式: 
• Struts1 Action是单例模式并且必须是线程安全的,因为仅有Action的一个实例来处理所有的请求。单例策略限制了Struts1 Action能作的事,并且要在开发时特别小心。Action资源必须是线程安全的或同步的。 
• Struts2 Action对象为每一个请求产生一个实例,因此没有线程安全问题。(实际上,servlet容器给每个请求产生许多可丢弃的对象,并且不会导致性能和垃圾回收问题) 

Servlet 依赖: 
• Struts1 Action 依赖于Servlet API ,因为当一个Action被调用时HttpServletRequest 和 HttpServletResponse 被传递给execute方法。 
• Struts 2 Action不依赖于容器,允许Action脱离容器单独被测试。如果需要,Struts2 Action仍然可以访问初始的request和response。但是,其他的元素减少或者消除了直接访问HttpServetRequest 和 HttpServletResponse的必要性。 

可测性: 
• 测试Struts1 Action的一个主要问题是execute方法暴露了servlet API(这使得测试要依赖于容器)。一个第三方扩展--Struts TestCase--提供了一套Struts1的模拟对象(来进行测试)。 
• Struts 2 Action可以通过初始化、设置属性、调用方法来测试,“依赖注入”支持也使测试更容易。 

捕获输入: 
• Struts1 使用ActionForm对象捕获输入。所有的ActionForm必须继承一个基类。因为其他JavaBean不能用作ActionForm,开发者经 常创建多余的类捕获输入。动态Bean(DynaBeans)可以作为创建传统ActionForm的选择,但是,开发者可能是在重新描述(创建)已经存 在的JavaBean(仍然会导致有冗余的javabean)。 
• Struts 2直接使用Action属性作为输入属性,消除了对第二个输入对象的需求。输入属性可能是有自己(子)属性的rich对象类型。Action属性能够通过 web页面上的taglibs访问。Struts2也支持ActionForm模式。rich对象类型,包括业务对象,能够用作输入/输出对象。这种 ModelDriven 特性简化了taglib对POJO输入对象的引用。 

表达式语言: 
• Struts1 整合了JSTL,因此使用JSTL EL。这种EL有基本对象图遍历,但是对集合和索引属性的支持很弱。 
• Struts2可以使用JSTL,但是也支持一个更强大和灵活的表达式语言--"Object Graph Notation Language" (OGNL). 

绑定值到页面(view): 
• Struts 1使用标准JSP机制把对象绑定到页面中来访问。 
• Struts 2 使用 "ValueStack"技术,使taglib能够访问值而不需要把你的页面(view)和对象绑定起来。ValueStack策略允许通过一系列名称相同但类型不同的属性重用页面(view)。 

类型转换: 
• Struts 1 ActionForm 属性通常都是String类型。Struts1使用Commons-Beanutils进行类型转换。每个类一个转换器,对每一个实例来说是不可配置的。 
• Struts2 使用OGNL进行类型转换。提供基本和常用对象的转换器。 

校验: 
• Struts 1支持在ActionForm的validate方法中手动校验,或者通过Commons Validator的扩展来校验。同一个类可以有不同的校验内容,但不能校验子对象。 
• Struts2支持通过validate方法和XWork校验框架来进行校验。XWork校验框架使用为属性类类型定义的校验和内容校验,来支持chain校验子属性 

Action执行的控制: 
• Struts1支持每一个模块有单独的Request Processors(生命周期),但是模块中的所有Action必须共享相同的生命周期。 
• Struts2支持通过拦截器堆栈(Interceptor Stacks)为每一个Action创建不同的生命周期。堆栈能够根据需要和不同的Action一起使用。

分享到:
评论

相关推荐

    struts1原理,struts2原理,spring mvc原理,struts1和struts2,struts和spring mvc

    **Struts2与Spring MVC比较:** 1. **灵活性**:Spring MVC允许更多的自定义,如自定义拦截器、视图解析器,而Struts2的扩展性相对弱些。 2. **依赖注入**:Spring MVC是Spring框架的一部分,天然支持DI,而Struts2...

    struts1和struts2的区别

    ### Struts1与Struts2的主要区别 #### 概述 Apache Struts 是一个用于构建企业级Java Web应用的开源框架。它分为两个版本:Struts1 和 Struts2。虽然两者都基于模型-视图-控制器(MVC)设计模式,但它们之间存在...

    Struts2与Struts1区别

    Struts2 和 Struts1 是两个著名的 Java Web 开发框架,它们都出自 Apache Software Foundation,但有着显著的区别。Struts1 是早期的 MVC 框架,而 Struts2 则是在 WebWork 框架的基础上发展起来的,它吸收了 Struts...

    Struts1与Struts2原理 区别详解汇总

    ### Struts1与Struts2原理及区别详解 #### Struts1原理概述 **Struts1** 是一种基于MVC架构的开源Java Web框架,它主要用于构建动态网站和应用程序。Struts1的核心组件包括ActionServlet、ActionForm以及Action...

    Struts1和Struts2区别

    Struts1和Struts2是两个非常著名的Java Web框架,它们都由Apache软件基金会开发,用于构建MVC(Model-View-Controller)架构的应用程序。虽然它们在目标上相似,但在设计模式、功能特性和使用体验上存在显著差异。...

    转:struts1与struts2的区别

    ### Struts1与Struts2的主要区别 #### 1. Action类的设计差异 - **Struts1**: 在Struts1框架中,Action类必须继承自一个抽象类`org.apache.struts.action.Action`。这种方式可能导致的问题是代码灵活性较低,尤其...

    Struts1_x与Struts2的深度比较

    Struts1_x与Struts2的深度比较

    Struts1和Struts2的区别和对比

    Struts1的Action由于与Servlet API紧密耦合,测试较为复杂,需要依赖如Struts TestCase这样的第三方工具。Struts2 Action则可以通过依赖注入和模拟对象进行测试,使其更易于测试。 在捕获用户输入方面,Struts1使用...

    Migrating From Struts1 To Struts2

    随着技术的发展与进步,Struts1作为曾经广泛使用的Web应用框架,逐渐被Struts2等新一代框架所取代。Struts2以其更加灵活的设计、丰富的特性以及更好的性能表现成为众多开发者的首选。本文将深入探讨如何从Struts1...

    struts2.0整合Struts 1

    1. **Action类与Result**:Struts 2中的Action类替代了Struts 1的Form Bean,负责处理用户请求并返回结果。Result则定义了Action执行后的跳转逻辑,可以是页面、重定向或任何其他响应。 2. **拦截器(Interceptor)...

    Struts2和Struts1的区别,详细点,主要说说Struts2

    ### Struts2与Struts1的主要区别 #### Struts2架构设计与实现方式 - **Struts2**在设计上采用了更加灵活的方式,每个请求都对应一个实例化的Action对象,而不是像**Struts1**那样在整个请求周期内共享同一个Action...

    struts1和struts2的jar包

    Struts1和Struts2是两个不同的版本,它们各自拥有独特的特性和功能,但在Java Web开发领域都扮演了重要角色。 **Struts1** Struts1是最早的版本,它在2001年发布,是基于ApacheJakarta项目的一个框架。Struts1的...

    struts1和struts2的区别(详细)

    ### Struts1与Struts2的主要区别 #### 一、Action执行机制的不同 - **Struts1**: 在Struts1框架中,Action是基于单例模式的,这意味着所有的请求都会共享同一个Action实例。这就导致了如果在Action中保存实例变量...

    struts1&struts2

    Struts1和Struts2是两个著名的MVC框架,它们都是Apache软件基金会的Apache Struts项目的一部分,用于构建基于Java的Web应用程序。然而,两者在设计和实现上有显著的差异。 1. **架构模式的区别** - **Struts1.x** ...

    struts1 和 struts2所需jar包

    struts1 和 struts2所需jar包。主要包含以下内容: struts-1.3.10-all.zip struts-1.3.10-apps.zip struts-1.3.10-lib.zip struts-1.3.10-src.zip struts-2.3.4.1-all.zip struts.rar

    struts与struts2的区别

    而Struts2则是在Struts1的基础上进行了全面改进,采用了拦截器(Interceptor)机制,使得处理请求的方式更加灵活和模块化。 **2. 拦截器:** Struts2的核心就是拦截器,它们是基于责任链模式实现的,可以进行一系列...

    Struts1_Struts2

    Struts1 和 Struts2 是两个非常著名的Java Web框架,由Apache软件基金会开发,用于构建企业级的MVC(Model-View-Controller)应用程序。这两个框架在Java开发领域有着广泛的应用,极大地提高了开发效率和代码的可...

    struts2jar包

    1. **struts2-core.jar**:这是Struts2的核心库,包含了框架的主要组件,如Action、Result、Interceptor等。它定义了请求处理的流程,提供了ActionContext、ValueStack等关键对象。 2. **xwork-core.jar**:XWork是...

Global site tag (gtag.js) - Google Analytics