Introduction
Often, we have multiple submit buttons within a single form. The below is just a simple way of identifying which button was clicked, and which actions to take.
There are, of course, many ways of doing this, including the use of javascript to identify the actions, etc... You're welcome to pick and choose whichever method you find most useful. Webwork is flexible enough.
Form
<button type="submit" value="Submit" name="submit">
<button type="submit" value="Cancel" name="cancel">
Action with boolean properties
class MyAction extends ActionSupport {
private boolean submitbutton;
private boolean cancelbutton;
public void setSubmitbutton( boolean submitbutton ) {
this.submitbutton = submitbutton;
}
public void setCancelbutton( boolean cancelbutton ) {
this.cancelbutton = cancelbutton;
}
public String execute() {
if ( submitbutton ) {
doSubmit();
return "submitResult";
}
if ( cancelbutton ) {
doClear();
return "cancelResult";
}
return super.execute();
}
}
Explanation
The boolean properties 'submit' and 'cancel' will be set to 'true' or 'false' according weather the submit or clear form element is present in the submitted form.
In this case, the properties are boolean, therefore the values set would be boolean.
There is another method, using String properties, described below...
Form
<button type="submit" value="Submit" name="submitbutton">
<button type="submit" value="Cancel" name="cancelbutton">
Action with String properties
class MyAction extends ActionSupport {
private String submitbutton; private String cancelbutton;
public void setSubmitbutton( String submitbutton ) {
this.submitbutton = submitbutton;
} public void setCancelbutton( String cancelbutton ) {
this.cancelbutton= cancelbutton;
}
public String execute() {
if ( "Submit".equals( submitbutton ) ) {
doSubmit();
return "submitResult";
}
if ( "Cancel".equals( cancelbutton ) ) {
doClear();
return "cancelResult";
}
return super.execute();
}
}
Explanation
In this case, the properties are String, therefore the values set are also String in nature.
I don't really like this method, as it ties in the Action to the Form. (What happens if you want different text to show up on the button ? You would have to change both the form as well as the corresponding action.)
Warning
- Field names are all in small caps because xwork seems to fail and search the web page otherwise. Values get set to null when not in small caps
- Field names of the buttons must be different from each other. Although having one name for all your submit buttons work in Firefox, it does not with Internet Explorer (IE). The difference is that Firefox returns only the value of the submit button pressed, while Internet Explorer would return the values of all elements with the given name.
Conclusion
There are other ways to achieve the same functionality. There are pros and cons to each methods. Feedback welcome.
|