Frustration with Struts 2 Wildcard Mappings
Published 1 year, 10 months agoSo, I finally got around to porting my little sample issue tracking app to Struts 2. I was looking forward to removing almost all of the mappings from my xwork.xml (now struts.xml) thanks to the new wildcard mapping functionality. However, I’ve run into a problem due to the fact that you cannot control the case of the matched values.
Here’s an example set of mappings that for a full CRUD on the Issue object. This pattern is repeated several times and seems like it should be replaceable with a wildcard mapping:
<action name=”issueList” class=”com.smitchelus.issue.action.IssueAction” method=”list”>
<result name=”success”>/WEB-INF/pages/issue/list.jsp</result>
</action><action name=”issueCreate” class=”com.smitchelus.issue.action.IssueAction” method=”create”>
<interceptor-ref name=”defaultStack”/>
<result name=”input” >/WEB-INF/pages/issue/edit.jsp</result>
<result name=”success”>/WEB-INF/pages/issue/edit.jsp</result>
</action><action name=”issueEdit” class=”com.smitchelus.issue.action.IssueAction” method=”edit”>
<interceptor-ref name=”defaultStack”/>
<result name=”input” >/WEB-INF/pages/issue/edit.jsp</result>
<result name=”success” >/WEB-INF/pages/issue/edit.jsp</result>
</action><action name=”issueSave” class=”com.smitchelus.issue.action.IssueAction” method=”save”>
<interceptor-ref name=”defaultStack”/>
<result name=”input” >/WEB-INF/pages/issue/edit.jsp</result>
<result name=”success” type=”redirect-action”>issueList</result>
</action>
So, let’s try to setup a wildcard mapping for the issue list page which would satisfy all list pages in the application:
<action name=”*List” class=”com.smitchelus.issue.action.{1}Action” method=”list”>
<result name=”success”>/WEB-INF/pages/{1}/list.jsp</result>
</action>
This works fine with the exception of the action class name which comes out as “issueAction” when the actual class name is “IssueAction”. I have not found any way to specify that the wildcard match should be init capped in the case of the action name.
If I make the wildcard mapping more like the examples in the documentation I run into another issue:
<action name=”list*” class=”com.smitchelus.issue.action.{1}Action” method=”list”>
<result name=”success”>/WEB-INF/pages/{1}/list.jsp</result>
</action>
By putting the verb before the noun I get the capital letter to construct the proper action class name, but now I have to init cap the directory that my JSPs live in!
So, I don’t have a solution yet. I will go post to the Struts mailing list and share if anything comes out of it. I’ve also started to dig through the XWork source, but at first blush I don’t see a solution hidden away there.
I wonder if it would be reasonable to have a special case with the action class name where it tried the current substitution logic, attempted to find the class and if it doesn’t exist then try the substitution again with the matched values init capped.
patriotlml on 7/17/2007
I got another problem some kind like yours.
I named my wild card in the following style:
——————————————-
/{1}/update{2}.jsp
/{1}/list{2}.jsp
——————————————-
And i try to access the action http://localhost:8080/myapp/common/User/list
Then I got the errors like:
no action mapping for namespace “/common/User” and action “list”.
I guess that my action has slashes,so I define:
However,it did not work either.
Any comment?
Thanks.
elevor on 10/9/2007
You have to set
struts.enable.SlashesInActionNames = truein the struts.properties file or as aconstantin your struts.xml file.See if that helps …
Matthew Payne on 1/10/2008
Maybe the
“default-class-ref” xml tag will help with the flexibility of configurations.
re:
http://www.sutternow.com/blog/index.action?date=2008-01-10
dbs on 6/18/2008
Just wanted to say that one of your comments just solved a problem i was working on. Thanks!