#!/bin/sh
curl 'https://github.com/technomancy/leiningen/raw/stable/bin/lein'>~/bin/lein
cd ~/bin;chmod a+x lein
mkdir ~/clojureroot
cd ~/clojureroot;lein new clojure-1.2;cd clojure-1.2;lein deps
Google Web ToolKits and Grails
Monday, January 3, 2011
Wednesday, September 15, 2010
GWT and Grails (II) with JSON
The previous post http://gwtgrails.blogspot.com/ demonstrated GWT insteracting with server service provided by Grails.
This post demonstrates how to access JSON data source. I will use grails to render a JSON response for demonstration purpose. The code is just to demonstrate how GWT access JSON and use the data, not trying to make the code to solve actual computing problem correctly.
1. Prepare JSON data source
a. grails create-domain-class Stock
This create the Stock.java class
b. add variables to Stock.java
class Stock {
String name;
String symbol;
Double price;
long id;
}
2. create controller
a. grails create-controller jsondemo
This create
grails-app/controllers/grailsgwtdemo/JsondemoController.groovy
b. add method to render a JSON
import grails.converters.JSON;
class JsondemoController {
def index = { }
def stocks={
List results=new ArrayList()
(0..10).each{
def data=new Stock()
data.id=it
data.symbol="s"+it
data.name="name"+it
data.price=it
results.add(data)
}
render results as JSON
}
}
3. Test json data source is working:
a. start grails
grails run-app
b. run url: http://localhost:8080/grailsgwtdemo/jsondemo/stocks
4. Access json from client client.java
a. Add a class src/gwt/com/demo/client/StockData.java
package com.demo.client;
import com.google.gwt.core.client.JavaScriptObject;
class StockData extends JavaScriptObject { // [1]
// Overlay types always have protected, zero argument constructors.
protected StockData() {} // [2]
// // JSNI methods to get stock data.
public final native String getSymbol() /*-{ return this.symbol; }-*/; // [3]
public final native double getPrice() /*-{ return this.price; }-*/;
public final native String getName() /*-{ return this.name; }-*/;
// // Non-JSNI method to return change percentage. // [4]
public final double getChangePercent() {
return 100.0 *0.5/getPrice();
}
}
This way, the code looks like pure java.
b. add these to the client.java
import com.google.gwt.core.client.JsArray;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
private static final String JSON_URL = "http://localhost:8080/grailsgwtdemo2/jsondemo/stocks";
String url = URL.encode(JSON_URL);
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
try {
Request request = builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
displayError("Couldn't retrieve JSON"+exception.toString());
}
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
updateTable(asArrayOfStockData(response.getText()));
} else {
displayError("Couldn't retrieve JSON (" + response.getStatusText() + ")");
}
}
});
} catch (RequestException e) {
displayError("Couldn't retrieve JSON:"+e.toString());
}
}
/*
* Convert the string of JSON into JavaScript object.
*/
private final native JsArray asArrayOfStockData(String json) /*-{
return eval(json);
}-*/;
/**
** Update the Price and Change fields for all rows in the stock table.
**
** @param prices Stock data for all rows.
**/
private void updateTable(JsArray prices) {
for (int i = 0; i <>
updateTable(prices.get(i));
}
lastUpdatedLabel.setText("total num of stocks returned : "+prices.length());
}
private void displayError(String error) {
errorMsgLabel.setText("Error: " + error);
errorMsgLabel.setVisible(true);
}
private void updateTable(StockData price) {
stockLabel.setText(stockLabel.getText()+":"+price.getSymbol());
}
Follow:
http://code.google.com/webtoolkit/doc/latest/tutorial/JSON.html
Monday, September 13, 2010
GWT and Grails
I spend some time tried and figured out how the GWT and Grails actually work together. So I keep it here so I can refer it later and may also be helpful to some one else.
1. install GWT
a. wget http://google-web-toolkit.googlecode.com/files/gwt-2.0.4.zip
b. unzip
c. setenv GWT_HOME
2. install grails just download grails install package from grails.org (I am using grails 1.3.4)
unzip it to some directory
setenv GRAILS_HOME
3. Create the grails project
grails create-app grailsgwtdemo
4. goto the grails project directory
cd grailsgwtdemo
5. install grail gwt plugin
grails install-plugin gwt
6. create the gwt module
grails create-gwt-module com.demo.client
The following two files created
./src/gwt/com/demo.gwt.xml
./src/gwt/com/client/demo.java
7. create gwt module hosting page
grails create-gwt-page index.gsp com.demo.client
This create the hosting page
web-app/index.gsp
8. try run it:
a. run web in one env:
grails run-app
b. run gwt client in another env:
grails run-gwt-client
9. Now try to do some interaction with services on the server side provided in grails
9.1 Create grails service:
grails create-service gwtdemo
This create the file:
grails-app/services/grailsgwtdemo/GwtdemoService.groovy
9.2 expose the service by adding this line into grails-app/services/grailsgwtdemo/GwtdemoService.groovy
static expose = [ 'gwt:com.demo.client' ]
9.3. Create the service method for the client:
String doit(String data){
"echo from server:"+data
}
Note: If you put def in place of the above return variable type String, the following autogenerated rpc code will have the Object.
9.4. grails generate-gwt-rpc
This generates the following 4 files:
src/java/org/codehaus/groovy/grails/plugins/gwt/client/GwtActionService.java
src/java/org/codehaus/groovy/grails/plugins/gwt/client/GwtActionServiceAsync.java
src/java/com/demo/client/GwtdemoService.java
src/java/com/demo/client/GwtdemoServiceAsync.java
9.5 Add this import
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
and annotation above the doit(String data) method
@RemoteServiceRelativePath("rpc")
9.6. add web-app/index.gsp in the body: Now is:
add these to the ./src/gwt/com/client/demo.java
imports:
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
variables:
private Label label=new Label();
private TextBox tb=new TextBox();
private Button button =new Button("click me!");
private static GwtdemoServiceAsync myService=GWT.create(GwtdemoService.class);
public void onModuleLoad() {
RootPanel.get("echo").add(label);
RootPanel.get("echo").add(tb);
RootPanel.get("echo").add(button);
button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
myService.doit(tb.getText(),new MyAsyncCallback(label));
}
}
);
}
public static class MyAsyncCallback implements AsyncCallback {
private Label mylabel;
public MyAsyncCallback(Label label){
this.mylabel=label;
}
public void onFailure(Throwable caught) {
this.mylabel.setText("sth wrong:"+caught.toString());
}
public void onSuccess(Object data) {
this.mylabel.setText((String)data);
}
}
10. Compile grails
grails compile
11. Compile gwt modules
grails compile-gwt-modules
12. run it again:
grails run-app
grails run-gwt-client
13. test on
http://localhost:8080/grailsgwtdemo/index.gsp
14. generate war file and deploy it
grails war
That's it. Pretty Straight forward.
1. install GWT
a. wget http://google-web-toolkit.googlecode.com/files/gwt-2.0.4.zip
b. unzip
c. setenv GWT_HOME
2. install grails just download grails install package from grails.org (I am using grails 1.3.4)
unzip it to some directory
setenv GRAILS_HOME
3. Create the grails project
grails create-app grailsgwtdemo
4. goto the grails project directory
cd grailsgwtdemo
5. install grail gwt plugin
grails install-plugin gwt
6. create the gwt module
grails create-gwt-module com.demo.client
The following two files created
./src/gwt/com/demo.gwt.xml
./src/gwt/com/client/demo.java
7. create gwt module hosting page
grails create-gwt-page index.gsp com.demo.client
This create the hosting page
web-app/index.gsp
8. try run it:
a. run web in one env:
grails run-app
b. run gwt client in another env:
grails run-gwt-client
9. Now try to do some interaction with services on the server side provided in grails
9.1 Create grails service:
grails create-service gwtdemo
This create the file:
grails-app/services/grailsgwtdemo/GwtdemoService.groovy
9.2 expose the service by adding this line into grails-app/services/grailsgwtdemo/GwtdemoService.groovy
static expose = [ 'gwt:com.demo.client' ]
9.3. Create the service method for the client:
String doit(String data){
"echo from server:"+data
}
Note: If you put def in place of the above return variable type String, the following autogenerated rpc code will have the Object.
9.4. grails generate-gwt-rpc
This generates the following 4 files:
src/java/org/codehaus/groovy/grails/plugins/gwt/client/GwtActionService.java
src/java/org/codehaus/groovy/grails/plugins/gwt/client/GwtActionServiceAsync.java
src/java/com/demo/client/GwtdemoService.java
src/java/com/demo/client/GwtdemoServiceAsync.java
9.5 Add this import
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
and annotation above the doit(String data) method
@RemoteServiceRelativePath("rpc")
9.6. add web-app/index.gsp in the body: Now is:
add these to the ./src/gwt/com/client/demo.java
imports:
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
variables:
private Label label=new Label();
private TextBox tb=new TextBox();
private Button button =new Button("click me!");
private static GwtdemoServiceAsync myService=GWT.create(GwtdemoService.class);
public void onModuleLoad() {
RootPanel.get("echo").add(label);
RootPanel.get("echo").add(tb);
RootPanel.get("echo").add(button);
button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
myService.doit(tb.getText(),new MyAsyncCallback(label));
}
}
);
}
public static class MyAsyncCallback implements AsyncCallback {
private Label mylabel;
public MyAsyncCallback(Label label){
this.mylabel=label;
}
public void onFailure(Throwable caught) {
this.mylabel.setText("sth wrong:"+caught.toString());
}
public void onSuccess(Object data) {
this.mylabel.setText((String)data);
}
}
10. Compile grails
grails compile
11. Compile gwt modules
grails compile-gwt-modules
12. run it again:
grails run-app
grails run-gwt-client
13. test on
http://localhost:8080/grailsgwtdemo/index.gsp
14. generate war file and deploy it
grails war
That's it. Pretty Straight forward.
Subscribe to:
Posts (Atom)