Getting Started With Project jMaki for the GlassFish v3 Application Server

Initializing the Widget With Data

The thisState and thisCity combobox widgets on the index.jsp page use EL expressions to get their initial set of data from StateBean. The following code snippet shows the jsp:useBean and widget tags from this page:

<jsp:useBean id="StateBean" scope="session"
	class="plotCity.StateBean" />
<a:widget id="thisState" name="dojo.combobox" ... 
	value="${StateBean.states}"/>
<a:widget id="thisCity" name="dojo.combobox" ...
	value="${StateBean.cities}" />

As the preceding markup shows, the data comes from the getStates method of StateBean. The useBean tag makes StateBean available to the index.jsp page.

In StateBean, the getStates method converts string arrays from Java code into a single JSON array:

public String getStates() throws JSONException {
	JSONArray statesData = new JSONArray();
	JSONObject stateData = new JSONObject();
	for (int loop = 0; loop < states.length; loop++) {
		stateData.put("label", states[loop]);
		stateData.put("value", stateCodes[loop]);
		statesData.put(stateData);
		stateData = new JSONObject();
	}
	return jmaki.util.JSONUtil.jsonArrayToString(statesData, new StringBuffer());
}

The getStates method uses the JSONArray API to create the following JSON array:

"[
	{label : 'Alaska', value : 'AK'},
	{label : 'Arizona', value : 'AZ'},
	{label : 'California', value : 'CA'},
	{label : 'Oregon', value : 'OR'},
	{label : 'Washington', value : 'WA'}
]"

Using the ${StateBean.states} expression, the combobox widget loads this JSON array.

The getCities method loads all the Alaskan cities into a JSON array because Alaska is the initially selected state.

Both methods call the jsonArrayToString method to convert a JSON array to a string before returning the data back to the page.

It's important to remember that the EL expressions can only be used to initialize the widgets with data. To update the widget's data, such as after a user-initiated event, you need to use the publish/subscribe mechanism, as described in the section, Handling Widget Events.