传统的struts开发由于需要编写太多配置文件致使开发效率始终无法提高,而ruby on rails那种基于契约的零配置不仅使我们惊叹
其实只要我们对struts稍加改造,也可以达到同样的效果:零配置
1.使用strus的wildcard mapping,提供类似rails的基于URL确定调用的Action与method
- 使用MappingDispathAction与扩展的DynamicModuleConfig,DynamicModuleConfig能够使用name属性作为formBean class的定义,以下为struts配置文件的全部
<action path="/example/*/*"
type="example.action.{1}Action"
name="example.form.{1}Form" scope="request"
validate="false" parameter="{2}" attribute="{1}Form">
</action>
第一个*为{1},代表需要调用的Action,每二个*为{2},代表我们要调用的action方法.
如:/example/User/save,则与下面等价
<action path="/example/User/save"
type="example.action.UserAction"
name="example.form.UserForm" scope="request"
validate="false" parameter="save" attribute="UserForm">
</action>
- 通过在Action的方法的直接返回ActionForward
而在Java Action代码中,我们直接使用new ActionForward()返回,在实际项目中使用些方法并末发现
return new ActionForward("/example/user/list.jsp");
2.使struts支持开发模式
现在的struts2中已经支持开发模式,在开发模式中,每个action请求将重新reload struts的所有配置文件及资源文件,避免由于类似国际化资源文件等的修改而需要reload整个应用(如tomcat中)
3.与spring结合,使用autowire,避免getBean()或是spring设置bean属性的到处调用
4.Struts Mock测试的支持
5.表单验证的改造
使用我的另外一个项目EasyValidation,提供类似下面的验证语法,并且不需要编写任何提示信息
<validation id="LoginForm">
<property name="username" validate="required min-length-6"/>
<property name="password" validate="required"/>
</validation>
验证的显示效果如下:
