Struts 1.x Vs Struts 2.x

原创 2007年09月13日 13:40:00
 
In the following section, we are going to compare the various features between the two frameworks. Struts 2.x  is very simple in comparison to the struts 1.x ,  few of its excelling features are:
1.  Servlet Dependency:
Actions in Struts 1  have dependencies on the servlet API since the HttpServletRequest and HttpServletResponse objects are passed to the execute method when an Action is invoked but in case of Struts 2 , Actions are not container dependent because they are made simple POJOs(由于Struts2的Action是简单的POJOs,所以Action不依赖于容器。). In struts 2, the servlet contexts are represented as simple Maps which allows actions to be tested in isolation. Struts 2 Actions can access the original request and response, if required. However, other architectural elements reduce or eliminate the need to access the HttpServetRequest or HttpServletResponse directly.
2.  Action classes
Programming the abstract classes instead of interfaces is one of design issues of struts1 framework that has been resolved in the struts 2 framework. (在Struts1框架设计中面向抽象类编程代替接口这一问题在Struts2中已经被解决。)
Struts 1 Action classes needs to extend framework dependent abstract base class. But in case of Struts 2 Action class may or may not implement interfaces to enable optional and custom services. In case of Struts 2 , Actions are not container dependent because they are made simple POJOs. 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.
3.  Validation

Struts 1 and Struts 2 both supports the  manual validation via a validate method.
Struts 1 uses validate method on the ActionForm, or validates through an extension to the Commons Validator. However, 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.
4.  Threading Model
In Struts 1, Action resources must be thread-safe or synchronized. So Actions are singletons and thread-safe, there should 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. However in case of 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.)
5.  Testability
Testing Struts1 applications are a bit complex. A major hurdle to test Struts 1 Actions is that the execute method because it exposes the Servlet API. A third-party extension, Struts TestCase, offers a set of mock object for Struts 1. But the Struts 2 Actions can be tested by instantiating the Action, setting properties, and invoking methods. Dependency Injection support also makes testing simpler. Actions in struts2 are simple POJOs and are framework independent,  hence testability is quite easy in struts2.
6.  Harvesting Input
Struts 1 uses an ActionForm object to capture input. And all ActionForms needs to extend a framework dependent base class. JavaBeans cannot be used as ActionForms, so the developers have to create redundant classes to capture input. 
However the Struts 2 uses Action properties (as input properties independent of underlying framework) that eliminates the need for a second input object, hence reduces redundancy. Additionally in struts2, 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. Even rich object types, including business or domain objects, can be used as input/output objects.
7.  Expression Language
Struts 1 integrates with JSTL, so it uses the JSTL-EL. The struts1 EL has basic object graph traversal, but relatively weak collection and indexed property support. Struts 2 can also use JSTL, however it supports a more powerful and flexible expression language called "Object Graph Notation Language" (OGNL).
8.  Binding values into views
In the view section, Struts 1 uses the standard JSP mechanism to bind objects(processed from the model section) into the page context to access . However 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 the reuse of views across a range of types which may have the same property name but different property types.
9.  Type Conversion
usually , Struts 1 ActionForm properties are all Strings. Struts 1 uses Commons-Beanutils for type conversion. These type converters are per-class, and not configurable per instance. However Struts 2 uses OGNL for type conversion. The framework includes converters for basic and common object types and primitives.
10.  Control Of Action Execution
Struts 1 supports separate Request Processors (lifecycles) for each module, but all the Actions in a module must share the same lifecycle. However 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.
 

相关文章推荐

Struts 2.x 与 struts 1.x 比较

Struts 2.x 与 struts 1.x 比较1).Action 类Struts 1.x 要求Action 类要扩展自一个抽象基类,struts 1.x 的一个共有的问题是面向抽象类编程而不是面...

Struts 1.x 与 Struts2 的区别

Struts1和Struts2的区别和对比   Struts1和Struts2的区别和对比  • Struts1要求Action类继承一个抽象基类。Struts1的一个普遍问题是使用抽...

Struts 1与Struts 2的区别比较

特征 Struts 1 Struts 2 Action类 Struts1的action需要去继承一个抽象基类。一个普遍问题就是Struts1是面向抽象类...

Struts 2整合Struts 1

  • 2010年09月18日 22:07
  • 5.12MB
  • 下载

freemarker for struts 1.x

  • 2009年11月06日 13:27
  • 14KB
  • 下载

Java大牛养成记(11)----Struts 2 & Struts 1

背景:每周一篇博客已经成了一种习惯,如果不就会感觉缺点什么,又是一周的最后一天了,写博客啊,但是这周好像没有研究过什么技术,加入框架组后分配了一项任务,就是搭建SSH框架,之前学习网上商城的时候学习了...

struts 2和struts 1的区别

Struts2从本质上来看源于Webwork,它同时具有Struts和Webwork的优点,并且使用了java1.5的注释为程序员提供了另外一种配置方案,虽然Struts2有许多优点,但它在使用上和S...

Struts 1与Struts 2区别

本文将从Struts1和Struts2的各个方面进行比较 本文仅供参考,原文可点击如下地址查看 http://struts.apache.org/2.0.11/docs/comparing...
  • Ldbiy
  • Ldbiy
  • 2014年11月11日 16:43
  • 732

虚拟访谈:Struts 1.x一路走好

本文来自于InfoQ中文站,原文地址是:http://www.infoq.com/cn/articles/virtual-panel-struts1。 众所周知,Apache不久前宣布Struts 1...
  • sqc1112
  • sqc1112
  • 2013年08月12日 11:28
  • 498

基本的struts 1.x工程讲解

1、工程创建     打开myEclipse,创建一个java web工程。点击工程右键添加struts特性。如图所示: 2、创建工程文件 在项目上右键,在弹出的对话框中选择New|...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Struts 1.x Vs Struts 2.x
举报原因:
原因补充:

(最多只允许输入30个字)