Comparing Struts 1 and 2

来源:互联网 发布:全球顶级域名服务器 编辑:程序博客网 时间:2024/05/21 18:31
FeatureStruts 1Struts 2Action classesStruts 1 requires Action classes to extend an abstract base class. Acommon(常规,普遍) problem in Struts 1 isprogramming(设计) to abstract classes instead of interfaces.An Struts 2 Action may implement an Action interface, along with other interfaces toenable(使..能够) optional and custom services. Struts 2 provides a base ActionSupport class to implementcommonly 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 requiresextra(额外的) 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 containersgenerate(产生) many throw-away objects per request, and one more object does not impose aperformance(性能) 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 assimple(简单的) Maps, allowing Actions to be tested in isolation(独立). Struts 2 Actionscan still(仍然可以) access the original(原始的,初始) request and response, if required. However, other architectural(架构) elementsreduce(减少) or eliminate(删除) the need to access the HttpServetRequest or HttpServletResponsedirectly(直接的).TestabilityA major(主要的 )hurdle(障碍) to testing Struts 1 Actions is that the execute methodexposes(暴露) 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 analternative(供选择) 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 pagevia(通过) the taglibs(标签). Struts 2 also supports the ActionFormpattern(模式), as well as POJO form objects and POJO Actions. Rich object types, including business(业务) ordomain(领域) 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(基本的对象结构遍历), butrelatively(相对) weak collection and indexed property support.Struts 2 can use JSTL, but the framework also supports a more powerful(强大的) andflexible(灵活的) expression language(表达式语言) called "Object Graph Notation Language" (OGNL).Binding values into viewsStruts 1 uses the standard(标准) JSP mechanism(机制,原理) forbinding(绑定) 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 allowsreuse(重新使用) 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.
原创粉丝点击