Welcome 微信登录

首页 / 软件开发 / JAVA / Struts2.1.6--想用通配符,不容易

Struts2.1.6--想用通配符,不容易2011-11-09 BlogJava stone2083初次使用Struts2,老老实实为每个action method配置url mapping文件。

时间长了,难为觉得繁琐,为何不使用COC的方式呢?终于,想到了使用通配符。

查看Struts2 Docs,找到相关配置方法:

<package name="alliance" namespace="/alliance" extends="struts- default">
<action name="*/*" class="cn.zeroall.cow.web.alliance.action.{1}Action" method="{2}">
<result name="target" type="velocity">/templates/alliance/{1}/ ${target}.vm</result>
<result name="success" type="velocity">/templates/alliance/{1}/ {2}.vm</result>
<result name="input" type="velocity">/templates/alliance/{1}/ {2}.vm</result>
<result name="fail" type="velocity">/templates/common/error.vm</result>
</action>
</package>

恩,非常方便,可是启动jetty,发现满足正则的url,就是找不到Action。

无奈,debug代码,找到原因,需要在struts.properties中,配置:

struts.enable.SlashesInActionNames = true

见注释:

### Set this to true if you wish to allow slashes in your action names. If false,
### Actions names cannot have slashes, and will be accessible via any directory
### prefix. This is the traditional behavior expected of WebWork applications.
### Setting to true is useful when you want to use wildcards and store values
### in the URL, to be extracted by wildcard patterns, such as
### <action name="*/*" method="{2}" class="actions.{1}"> to match "/foo/edit" or
### "/foo/save".