Date: 2004-09-19T10:37:33 Editor: MichaelMcGrady <[EMAIL PROTECTED]> Wiki: Apache Struts Wiki Page: StrutsCatalogMultipleButtonSolutions URL: http://wiki.apache.org/struts/StrutsCatalogMultipleButtonSolutions
no comment Change Log: ------------------------------------------------------------------------------ @@ -517,8 +517,79 @@ } ///;-) }}} == Button == +This solution, too, is only for <input type='image'> === Discussion === +This solution is as objectionable to me as (old) DispatchAction in Struts and its progeny, MappingDispatchAction and LookupDispatchAction. However, it is the first solution I proposed on this wiki, so I am putting it in here as a quasi-historical brain matter for those with quasi-hysterical brain matter. === SOLUTION FIVE: Button Code === +First we have the button: +{{{ +public final class ButtonCommand { + private ButtonCommand() { + } + + public final static String getCommand(HttpServletRequest request) { + Enumeration enum = request.getParameterNames(); + String parameterName = null; + while(enum.hasMoreElements()) { + parameterName = (String)enum.nextElement(); + if(parameterName.endsWith(".x")) { + return parameterName.substring(0,parameterName.indexOf('.')); + } + } + return parameterName; + } +} +}}} +Then we have the form: +{{{ +public class ButtonForm + extends ActionForm { + protected CrackWillowButton button; + protected String command; + + public CrackWillowButton getButton() { + if(button == null) { this.button = new CrackWillowButton(); } + return button; + } + + public String getCommand() { + String hold = command; command = null; + return hold; + } + + public void reset() { + button = null; + } + + public class CrackWillowButton { + private Integer x, y; + public CrackWillowButton() { } + public CrackWillowButton getSubmit() { command = "submit"; return button; } + public CrackWillowButton getClear() { command = "clear"; return button; } + + public void setX(Integer x) { + if(y != null) { reset(); } + else { this.x = x; } + } + + public void setY(Integer y) { + if(x != null) { reset(); } + else { this.y = y; } + } + } +} ///;-) +}}} +In the Action class, we check out which button type has been pressed as follows: +{{{ + String command = buttonForm.getCommand(); + if("submit".equals(command)) { + // do whatever + } else if ("clear".equals(command)) { + // do whatever + } else { + // some assert code + } +}}} == Links == --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]