Struts1和Struts2的区别比较



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

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

Struts2
使用 ”ValueStack” 技术,所以标签不用将视图和表现的对象结合就可以得到值 .ValueStack 策略允许通过一系列可能具有相同属性名字但是不同属性类型的的类型来完成视图的重用,
类型转换
Struts1 ActionForm 通常都是 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

Feature Struts 1 Struts 2
Action classes 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.
Threading Model 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.)
Servlet Dependency 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.
Testability 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.
Harvesting Input 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. 
Expression Language 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).
Binding values into views 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. 
Type Conversion 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.
Validation Struts 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 Execution 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.


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

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
1 在Action实现类方面的对比:Struts 1要求Action类继承一个抽象基类;Struts 1的一个具体问题是使用抽象类编程而不是接口。Struts 2 Action类可以实现一个Action接口,也可以实现其他接口,使可选和定制的服务成为可能。Struts 2提供一个ActionSupport基类去实现常用的接口。即使Action接口不是必须实现的,只有一个包含execute方法的POJO类都可以用作Struts 2的Action。 2 线程模式方面的对比:Struts 1 Action是单例模式并且必须是线程安全的,因为仅有Action的一个实例来处理所有的请求。单例策略限制了Struts 1 Action能做的事,并且要在开发时特别小心。Action资源必须是线程安全的或同步的;Struts 2 Action对象为每一个请求产生一个实例,因此没有线程安全问题。 3 Servlet依赖方面的对比:Struts 1 Action依赖于Servlet API,因为Struts 1 Action的execute方法中有HttpServletRequest和HttpServletResponse方法。Struts 2 Action不再依赖于Servlet API,从而允许Action脱离Web容器运行,从而降低了测试Action的难度。 当然,如果Action需要直接访问HttpServletRequest和HttpServletResponse参数,Struts 2 Action仍然可以访问它们。但是,大部分时候,Action都无需直接访问HttpServetRequest和HttpServletResponse,从而给开发者更多灵活的选择。 4 可测性方面的对比:测试Struts 1 Action的一个主要问题是execute方法依赖于Servlet API,这使得Action的测试要依赖于Web容器。为了脱离Web容器测试Struts 1的Action,必须借助于第三方扩展:Struts TestCase,该扩展下包含了系列的Mock对象(模拟了HttpServetRequest和HttpServletResponse对象),从而可以脱离Web容器测试Struts 1的Action类。Struts 2 Action可以通过初始化、设置属性、调用方法来测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值