File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Action Mapping - Wildcard Methods - Good or Bad? (Struts 2) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Action Mapping - Wildcard Methods - Good or Bad? (Struts 2)" Watch "Action Mapping - Wildcard Methods - Good or Bad? (Struts 2)" New topic

Action Mapping - Wildcard Methods - Good or Bad? (Struts 2)

James Gadbury

Joined: Mar 20, 2009
Posts: 24
Hi all,

I would be very interested to hear of different opinions, experiences and uses regarding wildcard mappings.

I started using Struts2 some months ago. Very quickly, I decided I would use wildcard mappings - they seemed neat and kept the struts2 configuration xml files tidy. I left validation of jsp pages until towards the end... Consider the following action mapping as an example:

I have the corresponding methods add() and edit() in my AccountAction class.

The trouble I found with wildcard mappings is that I can only map one result with the name "input". When validation failed, result name "input" was returned. If validation failed for add, I wanted to forward to add.jsp (to give the user another chance to add an account and display the errors). If validation failed for edit, I wanted to forward to edit.jsp. Therefore, I was unable to distinguish which page I wanted to go to when validation failed and I got result name "input" back from either of the action methods.

I found some solutions.

1) I can store the destination page in a variable in AccountAction. Therefore, when edit() is called, I could set the String inputDestination to "edit.jsp" and in my result mapping have:

Although my action must now know about jsp pages and their names, and set the correct input destination in each method.

This seems a little wrong to me... I'm working around an issue that shouldn't exist.

2) I experimented by writing a new Workflow interceptor. This basically returned "input" with "_methodName" appended, rather than just "input". So if validation of edit Account failed, my custom Workflow interceptor would return "input_edit" as the result name, allowing me to handle different validation failures. But this seemed wrong.

I am currently not using Wildcard mappings for this reason. Any thoughts on ways around this issue (or my own two proposals) would be greatly appreciated!

Thanks for reading!

David Newton

Joined: Sep 29, 2008
Posts: 12617

I'm not a big fan of wildcard mappings, although for simple usecases, they're okay. But I also don't like mashing a bunch of functionality into a single action, either, unless it's super-closely related. In the case you show first, though, you're not using the method name in the JSP result--the wildcards are available in the mappings just like action properties are, so "input" could be mapped to "{1}_account_input.jsp" or whatever.
I agree. Here's the link:
subject: Action Mapping - Wildcard Methods - Good or Bad? (Struts 2)
It's not a secret anymore!