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!


public abstract class BaseForm extends ValidatorForm { public static final int UNDEFINED = -1, SAVE = 1, DELETE = 3; private String saveButton = null; private String deleteButton = null; public String getSaveButton() { return saveButton; } public void setSaveButton(String saveButton) { this.saveButton = saveButton; } public String getDeleteButton() { return deleteButton; } public void setDeleteButton(String deleteButton) { this.deleteButton = deleteButton; } public int getSelectedButton() { if ((saveButton != null) && (!saveButton.equals(""))) { return SAVE; } else if ((deleteButton != null) && (!deleteButton.equals(""))) { return DELETE; } else { return UNDEFINED; } }Posted by Ted on May 03, 2004 at 06:23 PM MDT #
But have you given it a thought how those buttons could support i18N and not having to resort to scriptlet like HTML code for the buttons ?
I would be very eager to know how you see a solution for this ;-)
Posted by Werner Ramaekers on May 04, 2004 at 05:50 AM MDT #
Posted by Matt Raible on May 04, 2004 at 05:53 AM MDT #
input type="button" listener="ognl: listeners.doSave"> Where doSave - method in your PAGE class
Thats all. Very intuitive way IMHO. Tapestry the best :P
Posted by wassup on May 04, 2004 at 07:25 AM MDT #
But let me rephrase my question : I don't see how i can generate the HTML for the button using the Struts html tag library because there is no "name" attribute in the html:input tag. So in order for me to have i18n i can no longer use the Struts html taglib and i will need to use a scriptlet ?
Posted by Werner Ramaekers on May 04, 2004 at 12:30 PM MDT #
<html:submit styleClass="button"> <fmt:message key="button.submit"/> </html:submit>... is really the same as ...
<input type="submit" class="button" value="<fmt:message key="button.submit"/>" />Hmmm, it actually looks like the plain old HTML version requires less typing! Of course, if you're using HTML 4.0 compliant browsers, you could also do:
<button type="submit" class="button"> <fmt:message key="button.submit"/> </button>Of course, I could be wrong - do you know if <html:submit> buys you anything over plain ol' HTML?
Posted by Matt Raible on May 04, 2004 at 04:05 PM MDT #
becomes
Posted by Ted on May 04, 2004 at 05:55 PM MDT #
Posted by Javanese on May 04, 2004 at 10:05 PM MDT #
Posted by Matt Raible on May 04, 2004 at 10:38 PM MDT #
Posted by B. K. Oxley (binkley) on May 05, 2004 at 02:05 PM MDT #
as now its like : <.. href="user.do?action=edit" /> and in my lookupDispatchAction has:
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("notranslate.edit", "edit"); in my ResourceProperties.properties I have the property:
notranslate.edit=edit
Posted by Javanese on May 05, 2004 at 08:58 PM MDT #
Posted by Matt Raible on May 06, 2004 at 09:35 PM MDT #