Comparing Struts 1 and 2
Feature | Struts 1 | Struts 2 |
---|---|---|
Action classes | Struts 1 requires Action classes to extend an abstract base class. Acommon(常规,普遍) problem in Struts 1 is programming(设计) to abstract classes instead of interfaces. | An Struts 2 Actionmayimplement anActioninterface, along with other interfaces toenable(使..能够) optional and custom services. Struts 2 provides a base ActionSupport class to implementcommonly used(常用) interfaces. Albeit(虽然), the Action interface isnotrequired. Any POJO object with aexecutesignature 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 singletonstrategy(策略) 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 theexecutemethod when an Action is invoked. | Struts 2 Actions are not coupled(耦合) to a container. Mostoften(常常) the servlet contexts are represented assimple(简单的) Maps, allowing Actions to be tested inisolation(独立). Struts 2 Actions can still(仍然可以) access the original(原始的,初始) request and response, if required. However, otherarchitectural(架构) elements reduce(减少) or eliminate(删除) the need to access the HttpServetRequest or HttpServletResponsedirectly(直接的). |
Testability | A major(主要的 )hurdle(障碍) to testing Struts 1 Actions is that theexecutemethodexposes(暴露) 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 beredescribing(重写)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, includingbusiness(业务) or domain(领域) objects, can be used as input/output objects. The ModelDriven featuresimplifies(简化) taglb references to POJO input objects. |
Expression Language | Struts 1 integrates(结合) with JSTL, so it uses the JSTL EL. The EL has basicobject graph traversal(基本的对象结构遍历), but relatively(相对) weak collection and indexed property support. | Struts 2 can use JSTL, but the framework also supports a morepowerful(强大的) and flexible(灵活的) expressionlanguage(表达式语言) called "Object Graph Notation Language" (OGNL). |
Binding values into views | Struts 1 uses the standard(标准) JSPmechanism(机制,原理) for binding(绑定) objects into(到..里) the page context for access. | Struts 2 uses a "ValueStack" technology(技术) so that the taglibs can access values withoutcoupling(结合,耦合) your view to the object type it isrendering(表现). 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 includesconverters(转换器) for basic and common object types and primitives. |
Validation | Struts 1 supports manual validation(手工验证) via avalidatemethod on the ActionForm, or through anextension(扩展,延展) to the CommonsValidator(验证器). Classes can have different validation contexts for the same class, but cannot chain to validations on sub-objects. | Struts 2 supports manual validation via thevalidatemethod and the XWork Validation framework. The Xwork Validation Framework supportschaining(链接) 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(分配) RequestProcessors(处理) (lifecycles) for eachmodule(模块,组件), but all the Actions in the module must share the same lifecycle. | Struts 2 supports creating different lifecycles on aper(每) Action basis via Interceptor Stacks.Custom(自定义) stacks can be created and used with different Actions, as needed. |