Java Reference
In-Depth Information
Obviously, this solution is Prototype-specific. Although your favorite
Ajax framework may also have an equivalent feature, it's good to know
of a more general technique. One way is to include an
_eventName
request parameter with the name of the event handler, as in
_event-
Name=save
. Stripes uses that as the event name instead of looking at
the names of the other request parameters. We saw other ways of spec-
Besides taking multiple submit buttons into consideration, another
important issue with Ajax form submission is to make sure that the
JavaScript code returns
false
to prevent the browser from also submit-
ting the form in the traditional (non-Ajax) way. In our example,
send-
Money
( ) returns
false
, which becomes the return value in the
onclick=
event of the submit button:
function sendMoney(control) {
/
*
...
*
/
return false;
}
<s:submit name="doubleMoney" value="Send"
onclick="return sendMoney(this);"/>
With these issues out of the way, we're ready to respond differently
according to which button was clicked,
Send
or
Cancel
:
private static final
String RESULT = "/WEB-INF/jsp/result.jsp";
private static final
String CANCEL = "/WEB-INF/jsp/cancel.jsp";
public
Resolution doubleMoney() {
money =
new
Money(youGiveMe, youGiveMe
*
2);
return new
ForwardResolution(RESULT);
}
public
Resolution cancel() {
return new
ForwardResolution(CANCEL);
}
The
result.jsp
file is the same as before, but
cancel.jsp
gives a different
response to the user:
Fine then, keep your money!
This message is displayed below the form if the user clicks the
Cancel
Search WWH ::
Custom Search