博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts1和Struts2的区别比较
阅读量:4196 次
发布时间:2019-05-26

本文共 7207 字,大约阅读时间需要 24 分钟。

特征
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
根据需要都要有对应的栈被创建和使用。
 

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的开发将有很好的借鉴意义。

转载地址:http://crzli.baihongyu.com/

你可能感兴趣的文章
在中国提供了60亿次服务的疫情模块向世界开源
查看>>
世界卫生组织与腾讯加深合作 新冠肺炎AI自查助手全球开源
查看>>
Hibernate 中get, load 区别
查看>>
java反射详解
查看>>
JPA 注解
查看>>
JQuery 简介
查看>>
Java创建对象的方法
查看>>
Extjs自定义组件
查看>>
TreeGrid 异步加载节点
查看>>
Struts2 标签库讲解
查看>>
Google Web工具包 GWT
查看>>
材料与工程学科相关软件
查看>>
MPI的人怎么用仪器
查看>>
windows 下AdNDP 安装使用
查看>>
Project 2013项目管理教程(1):项目管理概述及预备
查看>>
ssh客户端后台运行
查看>>
哥去求职,才说了一句话考官就让我出去
查看>>
一位超算中心管理人员的空间
查看>>
Weapons of Mass Destruction, Detection
查看>>
环境资源与相关词汇中英文对照
查看>>