EasyJWeb vs Struts2
Posted 大峡
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EasyJWeb vs Struts2相关的知识,希望对你有一定的参考价值。
上周EasyJWeb-1.0m3版本发布,收到几封来信,其中有两封提到EasyJWeb相对于struts2这个web MVC框架有哪些优势。这两天在编《EasyJWeb实用开发指南》,因此,整理了一下其中的一些内容,对其中的一些关键作了一个简单的比较,希望对于想了解EasyJWeb及Struts2这两个框架的朋友有所帮助。
一、总体概述
1、都是基于java的开源web mvc框架,而且都是基于请求转发模型的mvc框架,这一点与jsf这种基于组件模型的框架不同。
2、EasyJWeb是在综合struts1.x、webwork、Tapestry、springmvc、rails等框架发展而来,是EasyJF开源团队的一个项目,开发成员全部是中国人;而struts2是在webwork2的基础上发展而来,当然也参考了很多mvc框架,是apache的一个开源项目,开发成员来自世界各地。
3、EasyJWeb表示层主要推Velocity、CommonTemplate这样的模板引擎,而Struts2视图层主要推jsp2。当然,两者都可以表示层切换到其它一些视图技术,只需要增加相应的结果处理引擎即可。
4、EasyJWeb的文档、代码注释、提示信息等主要以中文为主;而struts2是以英文为主。
5、都是一个能让你快速开发企业级java web应用的框架。
宏观的东西不多说,大的用法大家可以从两个框架与JPA+Spring结合实现的添删改查实例进行简单的对比分析。你可以直接通过下面的链接了解更多的内容:
Easyjweb+Spring2+JPA 实现一个基本CRUD应用示例 英文版
Struts 2 + Spring 2 + JPA + AJAX
Migrating Struts Apps to Struts 2
二、配置
1、零配置
EasyJWeb与struts2都支持零配置,这里的零配置就是指只需要修改web.xml,而不需要写任何EasyJWeb或Struts2的配置文件,零配置的情况下需要为框架指明到哪儿去扫描Controller(Action)类。分别如下:
EasyJWeb的web.xml
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
Struts2的web.xml
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
2、惯例代替配置
EasyJWeb中大量使用了惯例代替配置的机制,把应用程序按模块进行划分,惯例代替配置包含页面视图模板的查找,多国语言属性文件的查找、依赖注入等,已经在很多项目中成功应用。struts2也使用了一些惯例代替配置,但不知道能否用于实际开发。
3、框架配置文件
两个框架都可以有自己的配置文件,通过配置文件来提高系统的可维护性。EasyJWeb的配置文件中可以定义系统中的模块Module(相当于控制器或Action)、页面模板视图Page等MVC框架特有的属性,另外还可以配置业务组件<bean>、拦截器、异常处理器、及依赖注入,配置将要支持远程Web调用服务的Ajax业务组件等;EasyJWeb的配置文件功能更广,初学有一点大杂烩的感觉。
Struts2的配置文件同样可以用来配置Action、页面导向、拦截器,也可以配置业务组件Bean等,这一点跟EasyJWeb非常类似。采用分包及拦截器链的方式组织应用程序中的各个部件。
EasyJWeb配置文件中配置Bean
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
Struts2配置文件中配置Bean
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
三、控制器Action
两个框架的控制器在默认情况下都是线程安全的,而且都不依赖于具体的类。EasyJWeb默认的处理器实现要求Actionn必须实现IWebAction接口,而Struts2可以不需要。如下:
EasyJWeb中的Action
![](https://image.cha138.com/20210515/733183914bc54c829920b4bee96257ad.jpg)
![](https://image.cha138.com/20210515/aad21dd2ac284a408c1f39fca4f88738.jpg)
![](https://image.cha138.com/20210515/35bbe36d148d4c37822bea1b0193bdb9.jpg)
![](https://image.cha138.com/20210515/19d19c30e4c241ecb7fb8e99b12bf198.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/3c7657db3f6049febd9ce73323aafdf8.jpg)
![](https://image.cha138.com/20210515/4adf6213ad0848b19e915fb9138541bc.jpg)
Struts2中的Action
![](https://image.cha138.com/20210515/733183914bc54c829920b4bee96257ad.jpg)
![](https://image.cha138.com/20210515/aad21dd2ac284a408c1f39fca4f88738.jpg)
![](https://image.cha138.com/20210515/35bbe36d148d4c37822bea1b0193bdb9.jpg)
![](https://image.cha138.com/20210515/19d19c30e4c241ecb7fb8e99b12bf198.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/3c7657db3f6049febd9ce73323aafdf8.jpg)
![](https://image.cha138.com/20210515/4adf6213ad0848b19e915fb9138541bc.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
当然,这个祼体Action基本上没任何作为,真正的Struts2版本的HelloAction可以写成如下:
![](https://image.cha138.com/20210515/733183914bc54c829920b4bee96257ad.jpg)
![](https://image.cha138.com/20210515/aad21dd2ac284a408c1f39fca4f88738.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/35bbe36d148d4c37822bea1b0193bdb9.jpg)
![](https://image.cha138.com/20210515/19d19c30e4c241ecb7fb8e99b12bf198.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/3c7657db3f6049febd9ce73323aafdf8.jpg)
![](https://image.cha138.com/20210515/35bbe36d148d4c37822bea1b0193bdb9.jpg)
![](https://image.cha138.com/20210515/19d19c30e4c241ecb7fb8e99b12bf198.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/3c7657db3f6049febd9ce73323aafdf8.jpg)
![](https://image.cha138.com/20210515/4adf6213ad0848b19e915fb9138541bc.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
另外,还需要在struts2的配置文件中增加大致如下的配置来指明这个success字符对应的文件路径:
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
但实际应用中,EasyJWeb的Action大多数都直接继承AbstractPageCmdAction,因为这个基础控制器提供了很多非常强大的功能,比如按惯例查找页面、灵活页面跳转及视图切换,动态特性的命令方法。同样为了使用Struts2中的很多功能,比如拦截器、依赖注入等都需要Action实现指定的接口,更多的时候直接继承ActionSuport类。
也就是说,一个半祼或全祼的Action在实际应用中很少用到。
四、依赖注入
两个框架都实现了依赖注入,都能自动往Action中自动注入所需要的业务组件。EasyJWeb的依赖注入直接依靠他内置的IoC容器,而Struts2的依赖注入依靠拦截器ActionAutowiringInterceptor进行注入。EasyJWeb的依赖注入是真正实现不要任何配置就能自动注入应用程序中各层(业务层、持久层等)中用到的业务组件的,你可以通过这个简单的添删改查实例来看见效果。
两者都可以直接借助第三方的IoC容器如Spring。下面是在EasyJWeb中使用Spring容器,需要在EasyJWeb的配置文件中加入下面的配置:
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
Struts2可以使用代理的方式,先在web.xml文件中通过下面的配置启动spring容器:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
然后在struts的配置文件使用指定struts.objectFactory为spring即可。
<constant name="struts.objectFactory" value="spring" />
另外一点区别是,EasyJWeb的Action都是由EasyJWeb的IoC容器管理,不需要用其它容器来管理,因为EasyJWeb的IoC容器会把其它容器中的业务组件注入到Action的相应属性中,你可以把业务组件同时放在EJB容器、Spring容器、或者Guice容器中。
关于如何在EasyJWeb中使用Guice容器,请参考>>
五、数据的处理
两个框架都抛弃了Struts1.x中那种需要借助ActionFrom来处理数据的机制,而采用更为先进的注入方式。EasyJWeb中的WebForm中提供了一个toPo方法,用来处理数据注入。
比如,为了在Action中注入Person对象:
EasyJWeb的Action代码中直接使用toPo方法手动注入,如:
![](https://image.cha138.com/20210515/733183914bc54c829920b4bee96257ad.jpg)
![](https://image.cha138.com/20210515/aad21dd2ac284a408c1f39fca4f88738.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/35bbe36d148d4c37822bea1b0193bdb9.jpg)
![](https://image.cha138.com/20210515/19d19c30e4c241ecb7fb8e99b12bf198.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/3c7657db3f6049febd9ce73323aafdf8.jpg)
![](https://image.cha138.com/20210515/35bbe36d148d4c37822bea1b0193bdb9.jpg)
![](https://image.cha138.com/20210515/19d19c30e4c241ecb7fb8e99b12bf198.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/3c7657db3f6049febd9ce73323aafdf8.jpg)
![](https://image.cha138.com/20210515/4adf6213ad0848b19e915fb9138541bc.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
对应的表单是普通的html表示,如下所示:
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
Struts2中则不需要写代码,只需要把要注入的数据在Action中准备好即可,如:
![](https://image.cha138.com/20210515/733183914bc54c829920b4bee96257ad.jpg)
![](https://image.cha138.com/20210515/aad21dd2ac284a408c1f39fca4f88738.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/35bbe36d148d4c37822bea1b0193bdb9.jpg)
![](https://image.cha138.com/20210515/19d19c30e4c241ecb7fb8e99b12bf198.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/3c7657db3f6049febd9ce73323aafdf8.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/35bbe36d148d4c37822bea1b0193bdb9.jpg)
![](https://image.cha138.com/20210515/19d19c30e4c241ecb7fb8e99b12bf198.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/3c7657db3f6049febd9ce73323aafdf8.jpg)
![](https://image.cha138.com/20210515/35bbe36d148d4c37822bea1b0193bdb9.jpg)
![](https://image.cha138.com/20210515/19d19c30e4c241ecb7fb8e99b12bf198.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/3c7657db3f6049febd9ce73323aafdf8.jpg)
![](https://image.cha138.com/20210515/35bbe36d148d4c37822bea1b0193bdb9.jpg)
![](https://image.cha138.com/20210515/19d19c30e4c241ecb7fb8e99b12bf198.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/3c7657db3f6049febd9ce73323aafdf8.jpg)
![](https://image.cha138.com/20210515/35bbe36d148d4c37822bea1b0193bdb9.jpg)
![](https://image.cha138.com/20210515/19d19c30e4c241ecb7fb8e99b12bf198.jpg)
![](https://image.cha138.com/20210515/f553f861f1bd41f1a4e40cef8f69aed4.jpg)
![](https://image.cha138.com/20210515/3c7657db3f6049febd9ce73323aafdf8.jpg)
![](https://image.cha138.com/20210515/4adf6213ad0848b19e915fb9138541bc.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
对应的表单如下所示:
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
当然,Struts2关于数据转换方面提供了很多可供选择,比如也可以实现ModelDriven接口提供getModel来按指定的规则注入模型数据。
对于关联对像的加载及注入,比如Person中的Department属性,EasyJWeb只需要在属性上加上一个@POLoad标签即可,而struts2则不能。
六、其它
1、验证处理
EasyJWeb提供了一个基于注解标签实现的灵活框架,可以直接在要验证的数据对象(一般是模型对象)中直接添加验证标签来实现验证,另外在Action中加入自定义的验证内容也非常容易,只需要使用addError方法即可。当前EasyJWeb-1.0m3只支持通过注解的方式验证。
Struts2的验证框架结合了Strut1.x及webwork,可以直接使用验证标签来标注验证,也可以使用xml文件来配置验证规则及验证目标对象。
EasyJWeb的验证标签:
@Validator(name=”string”,value=”blank;trim;required;min:5;max:10;minMsg:最少不能少于5个字符;maxMsg:最大不能超过10字符”)
更多参考:http://www.easyjf.org/html/20070822/12867758-1954570.htm
Struts2的验证配置,
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)
![](https://image.cha138.com/20210515/b92e181273d74f8a8cd13774cc3ff432.jpg)