使用struts2中的codebehind插件

来源:互联网 发布:sql developer的set 编辑:程序博客网 时间:2024/05/18 20:51

转载:http://www.cnblogs.com/modou/articles/1301597.html

codebehind plugin是一个可以简化struts2配置的插件,虽然不能实现完全意义上的codebehind,至少已经做的不错了,期待新版本:-D

 

使用步骤:

1.确保你的应用已经可以使用struts2

2.添加struts2-codebehind-plugin-2.0.11.2.jar包到应用中

3.struts.xml中添加配置:<constant name="struts.codebehind.pathPrefix" value="/jsp/" /> 这里的value值填写jsp页面的根路径

 

然后就可以使用了,在不使用codebehind插件的时候,一般是这么写的:

<action name="userlist" class="userListAction">    <result>/jsp/userlist.jsp</result></action>

 

使用codebehind插件的话,可以这么写:

<action name="userlist" class="userAction" />

 

result可以省略了,当然这里有一个约定,返回页面的路径为struts.codebehind.pathPrefix + package namespace + action name + action returntype + .jsp

我这里的环境:

struts.codebehind.pathPrefix  = /jsp/

package namespace = /

action name = userlist

action returntype = 为success时,值为空,为其他时,值为"-" + return type

 

所以返回的页面地址就为 /jsp/userlist.jsp

如果return type为input,地址就为 /jsp/userlist-input.jsp

 

配合表达式的使用,可以基本实现零配置:

<action name="*" class="{1}Action" />

 

这样写不仅可以简化配置,而且可以达到规范文件布局的目的。如果有特殊的需要,也完全可以与手写配置文件相结合,用起来已经很方便了。


原创粉丝点击