Struts 1 and 2

来源:互联网 发布:全球直播软件 编辑:程序博客网 时间:2024/05/22 03:35
FeatureStruts 1Struts 2Action classesStruts 1 requires(需要) Action classes to extend an abstract base class. Acommon(通用的) problem in Struts 1 is programming to abstract classesinstead 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 containersgenerate(产生) many throw-away objectsper(每一) 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 theexecute method when an Action is invoked.Struts 2 Actions are not coupled(耦合的) to a container. Most often the servlet contexts arerepresented(代表) as simple Maps, allowing Actions to betested(检测) inisolation(隔离). Struts 2 Actions can still access theoriginal(原有的) 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 theexecute 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 createredundant(多余的) classes to capture input. DynaBeans can used as analternative(替代) 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 pagevia(通过) the taglibs. Struts 2 also supports(支持) the ActionForm pattern, 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 morepowerful(强大的) andflexible(灵活的) expression(表达式) language called "Object Graph Notation Language" (OGNL).Binding values into viewsStruts 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 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 andprimitives(基本实体).ValidationStruts 1 supports manual(手动) validation(验证) via avalidate method on the ActionForm, or through anextension(扩展) 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(单独的)  RequestProcessors(处理器) (lifecycles) for eachmodule(模块), 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. 

成为action 类的不同 
s1:Struts 1要求action继承一个抽象的基类。普遍的问题是struts1用接口抽象类代替接口。
s2:Struts 2实现action接口或是struts提供了一个基类ActionSupport(实现了通用的接口),
                       这个action类不是必须的,任何一个pojo对象用execute 能成为struts的action对象。

线程
s1:struts 1的action是单一的和必须是线程安全的,由于它只有一个类来处理action所有的请求,action必须是线程安全或同步的
s2:Struts 2的action是已经为每个请求实例化了,所以没有线程安全。(servletS容器产生了许多请求对象)

servlet依赖
s1:struts 1的action依赖servlet线程,由于当action被调用的时候httpServletRequest和httpServletResponse是传参给了execute()方法。(测试需要依赖web容器)
s2:struts 2的action不耦合(依赖)这容器,大多数这servlet的上下文被包含在Maps中。(允许action的隔离测试)。

testability
s1:(一个障碍)测试struts 1的action是由execute()自曝的一个servlet线程。一个三方软件,提供了一组mock对象给struts 1
s2:struts 2能被测试通过一个action实例,设置它的属性和调用方法。依赖给与关系 也许简单

harvesting 输入
s1:struts 1用一个actionfrom对象捕获输入,像actions,所有的actionfroms必须继承一个基类,甚至其他的JavaBean不能作为actionform.开发常常给一个多余的类作为捕获输入
DynaBeans能被使用作为一个替换给创建一个常规的actionform类,但是开发人员会重新描述现有的JavaBean
s2:struts 2用action属性作为这输入属性,不需要这第二个输入对象。输入属性赋给了对象类型,这action属性能被访问从这web的标记。struts也支持这actionform格式,
以及pojo表对象和pojo action对象,加强了对象类型,包含了业务和定义对象。能被作为pojo输入输出对象。(action自己的属性封装请求参数,避免了使用其他的输入参数)

绑定值到视图
s1:struts 1使用标准的jsp机制为这绑定对象到页面的上下访问。
s2:struts 2使用了一个 valueStack(值栈) 技术,以至于标记 能访问这值,没有耦合视图和这对象绑定,valueStack 允许在用视图

类型转换
s1:struts 1 actionform属性是通常用字符串,struts 1用 Commons-Beanutils为类型转换
s2:struts 2 是用ONGL转换类型,这框架包含了转换为基本的和通用的类型.基本实体

验证
s1:struts 1 是手动验证通过validate()方法在actionform,或者通过一个扩展的给这通用的Validator。类有不同的验证上下文。
s1:struts 2 支持手动验证通过validate()方法和xwork验证框架。XWork验证框架支持使用对象属性所属的类和验证上下文定义的验证规则来验证对象的子属性

action 执行控制
s1:struts 1支持分开的请求来处理各自的模块,但是在这模块中的action必须分享相同的生命周期
s2:struts 2支持创建不同的生命周期为每个action 通过拦截器栈,自定义拦截能被创建和能被用来不同的actions.