Mapping buttons to methods
In AppFuse, I use Struts' LookupDispatchAction to map submit buttons to methods in my Actions. It's caused quite a headache for i18n, but Jaap provided a workaround and now everything works fine. However, as I did the Spring MVC implementation this weekend, I didn't have to do any complicated "button value -> method name" mapping. Part of it was because I didn't need to, but also because I discovered that you can easily just check if the button's name was passed in. Explaining this with code is probably easier. Let's say you have three buttons on a page:
The HTML code for the above buttons is:
<input type="submit" name="save" value="Save" /> <input type="submit" name="delete" value="Delete" /> <input type="submit" name="cancel" value="Cancel" />
Using the name as your key, you can easily check in your Action/Controller/etc. to see which button was clicked:
if (request.getParameter("save") != null) {
// do something
}
The nice thing about this is that it doesn't care what value you put on your button - just what you name it. It seems like all frameworks should use something like this, rather than a single parameter name (i.e. "method") that requires JavaScript on a button to change the method invoked. About a month ago, Rick Hightower mentioned that he uses a ButtonNameDispatcher for Struts. Rick, if you're reading this - I'm ready for that bad boy!



