Struts2.1.6--想用通配符,不容易
初次使用Struts2,老老實實為每個action method配置url mapping文件。時間長了,難為覺得繁瑣,為何不使用COC的方式呢?終于,想到了使用通配符。
查看Struts2 Docs,找到相關配置方法:
<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
見注釋:
### 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".
啟動,COC終于成功。
但是(又冒出一個但是),針對*/*正則的url mapping,如何做validation呢?
按照struts2的約定,是通過:
[package/]ActionName-${配置中的action name=""中的名字}-validation.xml
如何把"/"這個符號放入到${配置中的action name=""中的名字}呢?
"/"可不是一個合法的文件名。
比如,我要為AlliedMemberAction/doRegister做validation,那么約定的校驗文件名應該是:
cn/zeroall/cow/web/alliance/action/AlliedMemberAction-AlliedMember/doRegister-validation.xml
這個特殊符號,可難剎我也。
無奈,繼續debug,發現在代碼:
xwork框架中的,AnnotationActionValidatorManager:
String fileName = aClass.getName().replace('.', '/') + "-" + context + VALIDATION_CONFIG_SUFFIX;
return loadFile(fileName, aClass, checkFile);
}
思想斗爭后,由于我不喜歡使用*-*的pattern,更喜歡使用*/*pattern,只好修改了源碼:
String fileName = aClass.getName().replace('.', '/') + "-" + context.replace("/", "-") + VALIDATION_CONFIG_SUFFIX;
return loadFile(fileName, aClass, checkFile);
}
不清楚struts2官方怎么看待這個問題。
大家是否有更好的方案,請指教
posted on 2009-09-26 14:06 stone2083 閱讀(3658) 評論(5) 編輯 收藏 所屬分類: java