Writing a rich web interface in Java and server side code in ruby : pretty unsual isn’it ? But hey after all, web client interfaces get everyday more complex and are mix of technologies. Gwt sounds interesting since you write your complex web UI components like you’d write a Swing widget, mostly in Java.
1) Create a Gwt JSNI class to invoke your rails service.
(I grabbed this code from the GWT mailing list and modified it a little bit).Then declare it in your GWT module.
2) Enable rails to perform Json responses.Everything is explained on this blog entry.N.B: importing the json module is “superfluous” ;-) since it’s packed with rails 1.1
3) Modify ROR active record base class to dump themselves as a Json data structure.
Add the following method (the code comes from rails wiki):
4) In your rails controller, send a Json response when asked (note the call to the to_json method):
and in your GWT client, the asynchronous HTTP request-response:
Note that this solution is limited for the following reasons:
- It’s forbidden to do Java reflection in the GWT client. So you can’tinstantiate a JavaBean from a Json object in a generic way.I would have liked to add a type field in my Json data representationand have my Java code instantiate a bean.
- Gwt client limits usage of J2SE 1.4.2 to collection’s API and some java.lang classes. See runtime library support