Java Reference
In-Depth Information
So, what exactly is the data that the server sends as a response? Here
is the code for the action bean to which the form is sent:
package
stripesbook.action;
public class
HelloAjaxActionBean
extends
BaseActionBean {
public int
youGiveMe;
public
Resolution doubleMoney() {
return new
JavaScriptResolution(
new
Integer(youGiveMe
*
2));
}
}
Very simple:
youGiveMe
is received as a parameter, and
doubleMoney
( )
sends a response with 2Ă—the value. That's the interesting part:
Java-
ScriptResolution
, helped behind the scenes by its buddy
JavaScriptBuilder
,
converts a Java object into JavaScript code and returns it as a resolu-
tion so that the data can be turned back into a JavaScript object with
the
eval
( ) function.
Remember that the Ajax request is sent as the user types characters
into the text field, without clicking the submit button. However, serial-
izing the form with
form.serialize
( ) includes the
name=
of the submit but-
ton,
doubleMoney
, in the request parameters. That causes the request
to target the
doubleMoney
( ) event handler. It's not necessary to include
a button just to indicate which event handler we want to call; we'll talk
about that a little later. Right now I also want to point out that if you
do click the submit button, you get to see exactly what data is sent by
the
JavaScriptResolution
. For example, if you enter
42
, the response data
is
84;
. That's not very exciting, but that's really all that's needed to get
the value
84
in JavaScript.
Now that we've gotten our feet wet, let's try using a model object with
JavaScriptResolution
, such as an instance of this
Money
class:
package
stripesbook.model;
public class
Money {
private int
youGaveMe;
private int
andIGiveYou;
public
Money(
int
youGaveMe,
int
andIGiveYou) {
this
.youGaveMe = youGaveMe;
this
.andIGiveYou = andIGiveYou;
}
/
*
getters and setters...
*
/
}
Search WWH ::
Custom Search