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-
ifying the event name in the request on page 292 .
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:
Download ajax/web/WEB-INF/jsp/multiple_submits.jsp
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 :
Download ajax/src/stripesbook/action/MultipleSubmitActionBean.java
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:
Download ajax/web/WEB-INF/jsp/cancel.jsp
Fine then, keep your money!
This message is displayed below the form if the user clicks the Cancel
button, as illustrated in Figure 15.4 , on the next page.
 
 
Search WWH ::




Custom Search