12 February 2011

Cross-site referencing in GWT

Al salamo Alykom,

basic steps to do cross-site referencing:

1- add
<add-linker name="xs" /> 
to your .gwt.xml file
2- replace all GWT-RPC by JsonpRequestBuilder

JsonpRequestBuilder requestBuilder = new JsonpRequestBuilder();
requestBuilder.setCallbackParam("callback");
requestBuilder.requestObject("http://mydomain/servlet?key=value&key=value", new AsyncCallback<JsArray<com.me.json_objects.Employee>>()
{
    public void onFailure(Throwable caught)
    {
    }
    
    public void onSuccess(com.me.json_objects.Employee result)
    {
        com.me.java_objects.Employee  emp = JsonConverterUtil.convertObjct(result);
    }
});


2.a- find a lib that convert java objects returned from business layer (in servlets) to JSON objects (http://jackson.codehaus.org/ is a great, simple to use)
Very simple really:
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;


ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
String value = mapper.writeValueAsString( object);


2.b- Convert json objects to java objects in GWT using JavaScript Overlay Types so every JSON object coming from server will be represented using using a JavaScript Overlay class, and this object at first has a java object that accessed by your GWT code. so you will have to map JSON objects returned from JsonpRequestBuilder to reqular Java objects to use in your GWT code.

Sample json object:
package com.me.json_objects;

public class Employee
{
    protected Employee(){}
    
    public final String native getName() /*-{
        return this.name;
    }-*/;

    public final String native getLocation() /*-{
        return this.location;
    }-*/;
}

That maps to this java object:
package com.me.java_objects;

public class Employee
{

    private String name;
    private String location;
    
    public Employee(){}
    
    public void setName(String name)
    {
        this.name = name;
    }
    public String  getName() {
        return this.name;
    }

    public String getLocation(){
        return this.location;
    }
    
    public void setLocation(String loc)
    {
        this.location = loc;
    }
}

to test your code, fire up your server (say tomcat) on two different http ports, one for deploying the client appand the other to deploy the GWT application.. and try to import the gwt application into the client, .. it should work.

Two ports here because cross-site in most browsers are for app running in different hosts and/or ports.

I wish if I could provide code sample soon :)

good luck

2 comments:

zeitgravity said...

Where's the source for JsonConverterUtil ?

mhewedy said...

As I can remember, it is a Utility class to fill an jsonObject to javaObject.

You can write it yourself.

it might look like:

public class JsonConverterUtil
{
public static com.me.java_objects.Employee convertObjct(com.me.json_objects.Employee param)
{
com.me.java_objects.Employee ret = new com.me.java_objects.Employee();

ret.setName(param.getName);
// etc...

return ret;
}
}