In one of my last projects I needed multiple submit buttons for one form. The Struts2 website contains information about ways to accomplish this - see
http://struts.apache.org/2.x/docs/multiple-submit-buttons.html
There are two approaches:
One uses Strings which does work but can't be recommended because the Strings are fixed in code and it doesn't work with i18n (at least if your button name contains special characters like the German Umlaut).
The other approach is much more elegant (taken from the website mentioned above):
class MyAction extends ActionSupport {
private boolean submit;
private boolean clear;
public void setSubmit(boolean submit) {
this.submit = submit;
}
public void setClear(boolean clear) {
this.clear = clear;
}
public String execute() {
if (submit) {
doSubmit();
return "submitResult";
}
if (clear) {
doClear();
return "clearResult";
}
return super.execute();
}
}
The problem is: This just doesn't work because Struts2 can't convert the Strings provided by the request to booleans.
To make this solution work, change the code as follows:
private boolean submit = false;
private boolean clear = false;
public void setSubmit(boolean submit) {
this.submit = true;
}
public void setClear(boolean clear) {
this.clear = true;
}
I admit this is an ugly hack and not elegant (your IDE should tell you), but it works and does not destroy i18n.
Comments [0]