Struts1和Struts2的区别比较

来源:互联网 发布:十大炒股软件排名 编辑:程序博客网 时间:2024/05/16 06:11



特征
Struts 1
Struts 2
Action
Struts1action需要去继承一个抽象基类。一个普遍问题就是Struts1是面向抽象类编程来代替接口编程
Struts2action可以实现一个Action接口,也可以同时实现一些其他的接口来添置一些附加的,常用的服务。Struts2提供一个基类ActionSupport实现了一些常用的接口。虽然Action接口不是必须的。任何附带execute方法的POJO对象都可以作为Struts2action对象。
 
线程模型
Struts1action是单例的而且必须是线程安全的,因为该类会只有唯一一个引用来为action处理所有的请求。单例策略会限制Struts1action的功能以及需要扩展的额外的功能(The singleton strategy places restrictions on what can be done with Struts 1 Actions and requires extra care to develop)。Struts1action必须是线程安全的并且是同步的。
 
Struts2Action对象是针对每一个请求的,所以自然也就不存在线程安全问题了。(实践中,servlet容器给每一个请求产生许多丟弃的对象,并且不会导致性能和垃圾回收问题)
Servlet的依赖
 
Struts1Action依赖于Servlet API,因为当Action被调用的时候HttpServletRequestHttpServletResponse对象是通过execute方法进行处理的。
Struts2Action和容器的连接并不紧密。通常servlet上下文被描绘成简单的Map映射,允许Action被单独测试。当然,如果需要的话Struts2Action也可以通过访问初始的requestresponse来完成一些功能。然而,其他的一些架构元素导致降低或者删除了直接访问requestresponse的需求。
易测试性
测试Struts1Action有一个大障碍就是execute方法是直接暴露于servlet API的。
Struts2Action可以很容易的通过设置属性调用方法来进行测试。当然依赖注入的支持也使得测试变得简单。
输入处理

Struts1
使用一个ActionForm对象来获取用户的输入。和action一样,所有的ActionForm都必须继承自一个基类。因为其他的javaBean不能被用作ActionForm,开发者通常要写一些多余的类来获取用户输入。DynaBean可以被用做生成ActionForm类的一个选择,但是开发者需要对现有的javaBean进行重写。
Struts2使用Action属性作为输入属性,除掉了对于输入对象的需求。输入属性可以是一个拥有他自己的属性的对象。Action属性是通过标签和web页面交互。Struts2也支持ActionForm模型,就是POJOForm对象和POJOAction。多数的对象类型,包括商业逻辑对象和领域对象都可以作为输入/输入对象。模式驱动特征简化了标签和POJO输入对象的关系。
表达式语言
Struts1JSTL结合,所以他可以使用JSTLEL
Struts2也支持JSTL,但是这个框架也支持更加强大的表达式语言OGNL.
表现层和类型值的绑定
Struts1使用标准的JSP机制将对象绑定到page context来进行访问。

Struts2
使用”ValueStack”技术,所以标签不用将视图和表现的对象结合就可以得到值.ValueStack策略允许通过一系列可能具有相同属性名字但是不同属性类型的的类型来完成视图的重用,
类型转换
Struts1ActionForm通常都是String类型。Struts1通过Commons-Beanutils实现类型转换。
Struts2使用OGNL实现类型转换,框架包含了对基础和公共类型的转换器。
验证
 
Struts1支持通过ActionForm中的validate方法实现手工验证。也可以通过扩展通用的验证框架进行验证。对于同一个类可以有不同的验证,但是不能关联到子对象的验证。
Struts2也支持通过validate方法进行手工验证以及Xwork验证框架进行验证。Xwork验证框架支持将验证链接到子属性,子属性使用了为属性类型和验证上下文定义的验证。
 
Action执行的控制
Struts1支持为每一个模块分配请求处理(生命周期),但是一个模块中的所有Action必须分享相同的生命周期。
Struts2支持通过拦截器栈为每个Action创建不同的生命周期。通常对于不同的Action根据需要都要有对应的栈被创建和使用。
 http://struts.apache.org/2.0.11/docs/comparing-struts-1-and-2.html

FeatureStruts 1Struts 2Action classesStruts 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.Threading ModelStruts 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.)Servlet DependencyStruts 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.TestabilityA 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.Harvesting InputStruts 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. 
Expression LanguageStruts 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).Binding values into viewsStruts 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. 
Type ConversionStruts 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.ValidationStruts 1 supports manual validation via a 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.Control Of Action ExecutionStruts 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.


Struts2是WebWork的升级,而不是Struts 1.x的升级。虽然Struts 2提供了与Struts1.x的兼容,但已经不是Struts1.x的升级。对于已有Struts1.x开发经验的开发者而言,Struts1.x的开发经验对于Struts2并没有太大的帮助;相反,对于已经有WebWork开发经验的开发者而言,WebWork的开发经验对Struts2的开发将有很好的借鉴意义。

原创粉丝点击