TOTD #5: Loading data from beans in jMaki widgets
The
jMaki
tutorial from SWDP explained the different approaches to load your own data
into a jMaki widget. The jMaki widget models have
formalized since then and so the code there no longer works. This TOTD
explains how a combo box widget in a JSP page gets it data from a bean.
This TOTD uses NetBeans IDE configured with
jMaki plugin and
GlassFish.
- Create a new Web application project using
NetBeans IDE, enable "jMaki Framework" and use all the
defaults. Choose GlassFish as the "Server". - In the default generated "
index.jsp" page, drag-and-drop "
" in the "Dojo
ComboboxMain Content Area". - Replace the generated code with the following fragment
<jsp:useBean id="itemBean" scope="session" class="server.ItemValueBean"
/><br>
<a:widget name="dojo.combobox" value="${itemBean.value}"/><br>
jsp:useBeantag instantiates the bean "server.ItemValueBean"
in session scope.a:widgettag uses${itemBean.value}
expression to load the data by invokinggetValue()method from
the bean. - Add a new class to the project and name it "
ItemValueBean"
in the package "server". Replace the entire generated code with
the following:
package server;<br>
<br>
import org.json.JSONArray;<br>
import org.json.JSONException;<br>
import org.json.JSONObject;
Thepublic class ItemValueBean {<br>
public String getValue() {<br>
JSONArray value = new JSONArray();<br>
for (int i=0; i<2; i++) {<br>
try {<br>
JSONObject item = new JSONObject();<br>
item.put("name", "name" + i);<br>
item.put("label", "label" + i);<br>
value.put(item);<br>
} catch (JSONException ex) {<br>
ex.printStackTrace();<br>
}<br>
}<br>
<br>
try {<br>
return jsonArrayToString(value, null);<br>
} catch (JSONException ex) {<br>
ex.printStackTrace();<br>
}<br>
<br>
return null;<br>
}<br>
<br>
/**<br>
* Converts a JSON Object to an Object Literal<br>
*<br>
*/<br>
public String jsonToObjectLibertal(JSONObject jo, StringBuffer buff)
throws JSONException {<br>
if (buff == null)<br>
buff = new StringBuffer("{");<br>
else <br>
buff.append("{");<br>
JSONArray names = jo.names();<br>
for (int l=0; (names != null) && l < names.length(); l++) {<br>
String key = names.getString(l);<br>
String value = null;<br>
if (jo.optJSONObject(key) != null) {<br>
value = key + ":";<br>
buff.append(value);<br>
jsonToObjectLibertal(jo.optJSONObject(key), buff);<br>
} else if (jo.optJSONArray(key) != null) {<br>
value = key + ":";<br>
buff.append(value);<br>
jsonArrayToString(jo.optJSONArray(key),
buff);<br>
} else if (jo.optLong(key, -1) != -1) {<br>
value = key + ":" + jo.get(key) + "";<br>
buff.append(value);<br>
} else if (jo.optDouble(key, -1) != -1) {<br>
value = key + ":" + jo.get(key) + "";<br>
buff.append(value);<br>
} else if (jo.opt(key) != null) {<br>
Object obj = jo.opt(key);<br>
if (obj instanceof Boolean) {<br>
value = key + ":" +
jo.getBoolean(key) + "";<br>
} else {<br>
value = key + ":" + "'" +
jo.get(key) + "'";<br>
}<br>
buff.append(value);<br>
}<br>
if (l < names.length() -1) buff.append(",");<br>
}<br>
buff.append("}");<br>
return buff.toString();<br>
}<br>
<br>
public String jsonArrayToString(JSONArray ja, StringBuffer buff) throws
JSONException {<br>
if (buff == null)<br>
buff = new StringBuffer("[");<br>
else<br>
buff.append("[");<br>
<br>
for (int key=0; (ja != null) && key < ja.length(); key++) {<br>
String value = null;<br>
if (ja.optJSONObject(key) != null){<br>
jsonToObjectLibertal(ja.optJSONObject(key), buff);<br>
} else if (ja.optJSONArray(key) != null) {<br>
jsonArrayToString(ja.optJSONArray(key),
buff);<br>
} else if (ja.optLong(key, -1) != -1) {<br>
value = ja.get(key) + "";<br>
buff.append(value);<br>
} else if (ja.optDouble(key, -1) != -1) {<br>
value = ja.get(key) + "";<br>
buff.append(value);<br>
} else if (ja.optBoolean(key)) {<br>
value = ja.getBoolean(key) + "";<br>
buff.append(value);<br>
} else if (ja.opt(key) != null) {<br>
Object obj = ja.opt(key);<br>
if (obj instanceof Boolean) {<br>
value = ja.getBoolean(key)
+ "";<br>
} else {<br>
value = "'" + ja.get(key)
+ "'";<br>
}<br>
buff.append(value);<br>
}<br>
if (key < ja.length() -1) buff.append(",");<br>
}<br>
buff.append("]");<br>
return buff.toString();<br>
}<br>
}<br>
<br>
getValuemethods contains the logic to generate the
business data. In this case, the method generates the
data model
expected by ComboBox using
JSON APIs. This data can very well be generated by
creating a
Persistence Unit and querying a database using JPA or any other
mechanism.
ThejsonToObjectLibertalandjsonArrayToString
methods were originally posted
here.
These two methods are required because the JSON parser does not allow you to
create object literals but only JSON objects. By default these contain key :
value pairs where the keys are enclosed in double quotes which does not
match with the expected data model. - Deploy and Run the application. The browser windows shows the default
page with a drop-down list box. The expanded list box shows the items that
are added to the combo box.

Another way to populate jMaki widgets with your data (using JPA) is explained
here.
Technorati:
totd
jmaki
beans
glassfish
netbeans
- Login or register to post comments
- Printer-friendly version
- arungupta's blog
- 728 reads





