Skip to main content

TOTD #5: Loading data from beans in jMaki widgets

Posted by arungupta on August 30, 2007 at 6:20 AM PDT

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.

  1. Create a new Web application project using
    NetBeans IDE
    , enable "jMaki Framework" and use all the
    defaults. Choose GlassFish as the "Server".
  2. In the default generated "index.jsp" page, drag-and-drop "
    Dojo 
    Combobox
    " in the "Main Content Area".
  3. Replace the generated code with the following fragment



    <jsp:useBean id="itemBean" scope="session" class="server.ItemValueBean" 
    />

    <a:widget name="dojo.combobox" value="${itemBean.value}"/>



    jsp:useBean tag instantiates the bean "server.ItemValueBean"
    in session scope. a:widget tag uses ${itemBean.value}
    expression to load the data by invoking getValue() method from
    the bean.
  4. 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;



    import org.json.JSONArray;

    import org.json.JSONException;

    import org.json.JSONObject;
    public class ItemValueBean {

      public String getValue() {

        JSONArray value = new JSONArray();

        for (int i=0; i<2; i++) {

          try {

            JSONObject item = new JSONObject();

            item.put("name", "name" + i);

            item.put("label", "label" + i);

            value.put(item);

          } catch (JSONException ex) {

            ex.printStackTrace();

          }

        }



        try {

          return jsonArrayToString(value, null);

        } catch (JSONException ex) {

          ex.printStackTrace();

        }



        return null;

      }



      /**

      * Converts a JSON Object to an Object Literal

      *

      */

      public String jsonToObjectLibertal(JSONObject jo, StringBuffer buff)
    throws JSONException {

        if (buff == null)

          buff = new StringBuffer("{");

        else

          buff.append("{");

        JSONArray names = jo.names();

        for (int l=0; (names != null) && l < names.length(); l++) {

          String key = names.getString(l);

          String value = null;

          if (jo.optJSONObject(key) != null) {

            value = key + ":";

            buff.append(value);

           
    jsonToObjectLibertal(jo.optJSONObject(key), buff);

          } else if (jo.optJSONArray(key) != null) {

            value = key + ":";

            buff.append(value);

            jsonArrayToString(jo.optJSONArray(key),
    buff);

          } else if (jo.optLong(key, -1) != -1) {

            value = key + ":" + jo.get(key) + "";

            buff.append(value);

          } else if (jo.optDouble(key, -1) != -1) {

            value = key + ":" + jo.get(key) + "";

            buff.append(value);

          } else if (jo.opt(key) != null) {

            Object obj = jo.opt(key);

            if (obj instanceof Boolean) {

              value = key + ":" +
    jo.getBoolean(key) + "";

            } else {

              value = key + ":" + "'" +
    jo.get(key) + "'";

            }

            buff.append(value);

          }

          if (l < names.length() -1) buff.append(",");

        }

        buff.append("}");

        return buff.toString();

      }

     

      public String jsonArrayToString(JSONArray ja, StringBuffer buff) throws
    JSONException {

        if (buff == null)

          buff = new StringBuffer("[");

        else

          buff.append("[");



        for (int key=0; (ja != null) && key < ja.length(); key++) {

          String value = null;

          if (ja.optJSONObject(key) != null){

           
    jsonToObjectLibertal(ja.optJSONObject(key), buff);

          } else if (ja.optJSONArray(key) != null) {

            jsonArrayToString(ja.optJSONArray(key),
    buff);

          } else if (ja.optLong(key, -1) != -1) {

            value = ja.get(key) + "";

            buff.append(value);

          } else if (ja.optDouble(key, -1) != -1) {

            value = ja.get(key) + "";

            buff.append(value);

          } else if (ja.optBoolean(key)) {

            value = ja.getBoolean(key) + "";

            buff.append(value);

          } else if (ja.opt(key) != null) {

            Object obj = ja.opt(key);

            if (obj instanceof Boolean) {

              value = ja.getBoolean(key)
    + "";

            } else {

              value = "'" + ja.get(key)
    + "'";

            }

            buff.append(value);

          }

          if (key < ja.length() -1) buff.append(",");

        }

        buff.append("]");

        return buff.toString();

      }

    }



    The getValue methods 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.



    The jsonToObjectLibertal and jsonArrayToString
    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.
  5. 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

Related Topics >>