> Hi ,
>
> Moitra and Thomas
>
> Thanks for your reply.. !!
>
> Anyways i got a solution..
>
> *use this code in the servlet* ---
> - get the list of products (productList) and use it in the servlet:
> - This is JSON format to form a array of objects
>
> // Open the <script> tag
> writer.append("<script type=\"text/javascript\">");
> writer.append("var product=");
> writer.append("{productList: '[");
>
> Iterator<Product> iter = productList.iterator();
> while (iter.hasNext()) {
> Product product= iter.next();
> writer.append("{productName: \"" + Product .getProductName() +
> "\", id: " + Product .getId() +
> ", productDetails: \"" + Product .getProductDetails() +
> .................................
> .................................
> "\"}");
> if (iter.hasNext()) {
> writer.append(",");
> }
> }
>
> // End the <script> tag
> writer.append("]'}; </script>");
>
> *use this code in the GWT onModule *---
> - call the above servlet on onModule
>
> Dictionary product= Dictionary.getDictionary("product");
> String productListStr = product.get("productList").toString();
> JSONArray productJSONArray = JSONParser.parse(productListStr
> ).isArray();
> List<Product> ProductList = new ArrayList<Product>();
> for (int i = 0; i < productJSONArray .size(); i++) {
> JSONObject productJSONObject = (JSONObject)productJSONArray
> .get(i);
> Product product= new Product();
>
> JSONString productNameJSONString = productJSONObject
> .get("productName").isString();
> product.setProductName(productNameJSONString .stringValue());
>
> product.setId(Long.valueOf(productJSONObject
> .get("id").toString()));
>
> .........................
> .........................
>
> ProductList .add(product);
> }
>
> finally we will get a list of objects. i.e, ProductList .
>
> Guys its really cool concept .. !! enjoy it !! :-)
Rewritten using overlay types:
public final class Product extends JavaScriptObject {
protected Product() { /* required for overlay types */ }
public native String getProductName() /*-{ return
this.productName; }-*/;
public native String getId() /*-{ return this.id; }-*/;
}
...
public native JsArray<Product> getProductList() /*-{ return
$wnd.product.productList; }-*/;
...
JsArray<Product> productList = getProductList();
Of course, if your really want Product as a "real" bean, and a List<?>
instead of a JsArray<?>, you can copy things around (see below); but
really I believe overlay types are much more readable than anything
using com.google.gwt.json.* classes.
// renaming the above overlay type to ProductJSO, and using your
Product bean class:
List<Product> productList = new ArrayList<Product>();
JsArray<ProductJSO> products = getProductList();
for (int i = 0, l = products.getLength(); i < l; i++) {
ProductJSO jso = products.get(i);
Product p = new Product();
p.setProductName(jso.getProductName());
p.setId(jso.getId());
products.add(p);
}
--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
No comments:
Post a Comment