首页 > 代码库 > IT忍者神龟之Comparing Struts 1 and 2
IT忍者神龟之Comparing Struts 1 and 2
Comparing Struts 1 and 2
Edit Page Browse Space Add Page Add News
Feature | Struts 1 | Struts 2 |
---|---|---|
Action classes | Struts 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 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 assimple(简单的) 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 HttpServletResponsedirectly(直接的). |
Testability | A 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 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 createredundant(冗余) classes to capture input. DynaBeans can used as an alternative(供选择) to creatingconventional(符合) 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 viewsacross(通过,穿过) 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. |
IT忍者神龟之Comparing Struts 1 and 2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。