Commits

Anonymous committed f765fc5

added an example into showcase (showing the use of Dojo with WebWork's JSON result)

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@2999573baa09-0c28-0410-bef9-dab3c582ae83

  • Participants
  • Parent commits 5ca406b

Comments (0)

Files changed (5)

File webapps/showcase/src/java/com/opensymphony/webwork/showcase/json/CountryCitiesAction.java

+package com.opensymphony.webwork.showcase.json;
+
+import com.opensymphony.xwork.ActionSupport;
+import com.opensymphony.webwork.dispatcher.json.JSONObject;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.ArrayList;
+import java.util.Collections;
+
+/**
+ * @author tmjee
+ * @version $Date$ $Id$
+ */
+public class CountryCitiesAction extends ActionSupport {
+
+    private String country;
+    private JSONObject jsonObject;
+
+    private Map cities = new HashMap() {
+        {
+            put("uk", new ArrayList() {
+                {
+                    add("Manchester");
+                    add("London");
+                    add("Liverpool");
+                    add("Notthingham");
+                }
+            });
+            put("us", new ArrayList() {
+                {
+                    add("California");
+                    add("New York");
+                    add("Texas");
+                    add("Utah");
+                }
+            });
+            put("aus", new ArrayList() {
+                {
+                    add("Sydney");
+                    add("Melbourne");
+                    add("Adelaide");
+                    add("Victoria");
+                }
+            });
+            put("ger", new ArrayList() {
+                {
+                    add("Bamberg");
+                    add("Berlin");
+                    add("Cologne");
+                    add("Dresden");
+                }
+            });
+        }
+    };
+
+
+    public String getCountry() { return country; }
+    public void setCountry(String country) { this.country = country; }
+
+
+    public JSONObject getJsonObject() {
+        return jsonObject;
+    }
+
+    public String execute() throws Exception {
+
+        jsonObject = new JSONObject();
+        System.out.println("******* country="+country);
+        if (cities.containsKey(country)) {
+            jsonObject.put("cities", cities.get(country));
+        }
+        else {
+            jsonObject.put("cities", Collections.EMPTY_LIST);
+        }
+
+        return SUCCESS;
+    }
+}

File webapps/showcase/src/webapp/WEB-INF/classes/xwork.xml

     
     <include file="config-browser.xml"/>
 
+    <include file="xwork-json.xml" />
+
     <include file="xwork-dwr.xml" />
 
     <include file="xwork-messageStore.xml" />

File webapps/showcase/src/webapp/WEB-INF/decorators/main.jsp

                     <li><a href="<ww:url action="start" namespace="/flash"/>">Flash</a></li>
                     <li><a href="<ww:url action="changeLocale" namespace="/i18n"/>">I18N</a></li>
                     <li><a href="<ww:url action="index" namespace="/webwork_dwr"/>">DWR</a></li>
+                    <li><a href="<ww:url action="index" namespace="/json"/>">JSON</a></li>
                     <li class="last"><a href="<ww:url value="/help.jsp"/>">Help</a></li>
                 </ul>
             </div>

File webapps/showcase/src/webapp/json/index.jsp

+<%--
+  Created by IntelliJ IDEA.
+  User: tmjee
+  Date: Dec 16, 2007
+  Time: 11:43:27 PM
+  To change this template use File | Settings | File Templates.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<%@taglib prefix="ww" uri="/webwork" %>
+<html>
+  <head>
+      <title>Simple jsp page</title>
+      <ww:head theme="ajax" />
+      <script type="text/javascript">
+          dojo.require("dojo.json");
+          dojo.addOnLoad(function() {
+              dojo.event.connect(dojo.byId("country"), "onchange", function() {
+                    var selectTag = dojo.byId('country');
+                    var citiesSelectTag = dojo.byId("cities");
+                    //alert(selectTag.options[selectTag.selectedIndex].value);
+                    dojo.io.bind({
+                        method: "POST",
+                        url: "<ww:url action='countryCities' namespace='/json' />",
+                        content: {
+                            country: selectTag.options[selectTag.selectedIndex].value    
+                        },
+                        load: function(type, data, event) {
+                            try {
+                                var jsonObj = dojo.json.evalJson(data);
+
+                                // clear the cities select tag options
+                                citiesSelectTag.options.length = 0;
+
+                                if (jsonObj.cities) {
+                                    var cities = jsonObj.cities;
+                                    for (var a=0;a <cities.length; a++) {
+                                        var option = document.createElement("option");
+                                        option.text = cities[a];
+                                        option.value = cities[a];
+                                        citiesSelectTag.add(option, null);
+                                    }
+                                }
+                            }
+                            catch(e) {   // just in case there's an invalid JSON script
+                                alert('error evaluating JSON '+e);
+                            }
+                        },
+                        error: function(type, error){
+                            alert( "Error : " + error.message );
+                        },
+                        mimetype: "text/plain"
+                    });
+              });
+          });
+      </script>
+  </head>
+  <body>
+    <select id="country" name="country">
+        <option selected="selected" value="none">Please Select</option>
+        <option value="uk">United Kingdom</option>
+        <option value="us">United States</option>
+        <option value="aus">Australia</option>
+        <option value="ger">Germany</option>
+    </select>
+    <br/>
+    <select id="cities" name="cities">
+    </select>
+  </body>
+</html>

File webapps/showcase/src/webapp/showcase.jsp

         <li><ww:url id="url" action="showForm" namespace="/messageStore" /><ww:a href="%{url}">Message Store Eample</ww:a></li>
 
         <!-- WebWork / DWR integration -->
-        <li><ww:url id="url" action="index" namespace="/webwork_dwr" /><ww:a href="">WebWork DWR integration</ww:a></li>
+        <li><ww:url id="url" action="index" namespace="/webwork_dwr" /><ww:a href="%{url}">WebWork DWR integration</ww:a></li>
+
+        <!-- JSON -->
+        <li><ww:url id="url" action="index" namespace="/json" /><ww:a href="%{url}">JSON</ww:a></li>
 
     </ul>
 </p>