Archive for the ‘AJAX’ Category

POSTable HTML links

January 17, 2007

When building web sites you often have to produce POST request on user’s actions. Many times without having user – editable form fields. Server just expects you to send POST. The standard solution is to make hidden form, hardcode some fields value and dynamically populate other values when user presses submit button. It does the job. But the code is spoiled with these pseudo forms and, I believe, you never gonna like it.

There is other approach that is much more concise and programmer friendly (tm:) . The trick is to construct and submit form on-the-fly. Let’s start with JavaScript function that exploits Prototype:

XSLT + JSON-RPC as alternative to JAXB

July 17, 2006

Lately I use JSON-RPC extensively and not only for RPC communication. JSON-RPC library comes with few very useful helpers that made worth using it as standalone JSON-> Java conversion tool for really complex objects.
In one of projects I had to convert proprietary XML-formatted data to Java POJOs with number of calculations/transformation rules that made using of standard JAXB tools seamless. Each XML file comprised more than 1000 elements and resulting domain model had 20 classes organized in a tree. After some attempts to write annotation-based transformation engine(it actually worked but java code was poisoned with 2/3 of annotations. Moreover transformation rules were not changeable without recompilation) a decided to use XSLT + JSON-RPC chain.
First of all I wrote XSL templates for performing all transformation rules. Have to admit that it was mach easier to define rules with XPath/XQuery than in plain java(ok, there is common-collections + bean-utiles but it is nothing comparing to XSLT). As result of XSLT I had text file with my objects tree defined in JSON.
Second step was converting JSON to Java. JSON-RPC library is just brilliant for that. You need to get Bridge class, define mapping hints to Java classes, get serializer and you good to go.

JSONRPCBridge bridge = JSONRPCBridge.getGlobalBridge();
bridge.registerClass("com.maersk.tpd.domain.Route", com.maersk.tpd.domain.Route.class);
bridge.registerClass("com.maersk.tpd.domain.RouteLeg", com.maersk.tpd.domain.RouteLeg.class);
bridge.registerClass("com.maersk.tpd.domain.RoutePoint", com.maersk.tpd.domain.RoutePoint.class);
bridge.registerClass("com.maersk.tpd.domain.RoutePointRole", com.maersk.tpd.domain.RoutePointRole.class);
JSONSerializer serialiser = bridge.getSerializer();

Nice part that JSONRPCBridge has number of predefined serializers for various Java classes like Date, Float, Boolean, etc. However sometimes you need to add you own serialisers for custom types. The JSON-RPC guys definitely anticipated it so you can register you own serialiser on bridge with registerSerializer(Serializer s) method. Fair enough unless you want to redefine serialiser that handles already mapped class. Let say you execute myBridge.registerSerializer(new MyDateSerialiser()). What you get is “different serializer already registered” exception. No comments here. It is probably has some explanation.. So if you steel want to do it (I did) you have to add one more function to JSONSerialiser:

public void registerOrReplaceSerializer(Serializer s) throws Exception {
  Class classes[] = s.getSerializableClasses();
  synchronized (serializerSet) {
    if(!serializerSet.contains(s)) {
      if(isDebug())"registered serializer " + s.getClass().getName());
      for(int i=0; i < classes.length; i++) {
        serializableMap.put(classes[i], s);
      serializerList.add(0, s);

So now you might add your own serialisers and see the whole beast working:

serialiser.registerOrReplaceSerializer(new GcssDataSerialiser());
serialiser.registerOrReplaceSerializer(new GcssBooleanSerialiser());
Object obj = serialiser.fromJSON(jsonString);

Need to mention that both transformations XML-JSON and JSON->Java work extremely fast. So when the time to optimization came the transformation part was out of questions. Hibernate batches were most time consuming thing. But it is another story for next post.

Concrete objects with JSON-RPC

July 16, 2006

jsonJSON-RPC is one of the most used transport technologies in modern AJAX world. It has many advantages especialy comparing to fatty XHR. However it has .. not drawbacks but rather space for improvement. As you remember the JSON Object is a classic TO and typeless by nature.
Good enough for most cases but sometimes I want to transfer from server to client full-fledged objects that carry internal functionality. Say client-side Customer must have toString() function. So we need a way how to transform typeless JSON transfer objects to class of our domain model defined in JS. The solution is simple. We hint our Java class Customer with name of JS class the same way as we hint JS classes with name of Java class representation (see JSON-RPC manual for more details). On client side we need to cast JSON object to our model class with name taken from jsClass property. To cast? In JS? Yes indeed. myJsonObject.toString = Customer.prototype.toString will do the job if we have few functions. Let say we may define function Customer.decorate(obj) on Customer that will extend given object with all functionality of Customer. Otherwise we may use Prototype library’s metod Object.extend(myJsonObj, Customer) which will extend myJsonObj with all methods that Customer’s methods. So putting all together we have bidiractional Java-JS transfer of typed objects.