Prototype + X-JSON + Struts 2

I wrote a little action today which makes use of this so I thought I’d share it. The idea is to pass a JSON String back from the server side to a page which makes the call using Prototype and evaluates the response. The only thing unusual that’s happening is that the String from the server is not part of the response body but is instead supplied as a response header.

public String getPerson() {
  Person person = personService.getPerson(id);
  JSONObject obj = new JSONObject();
  obj.put("firstName", person.getFirstName());
  obj.put("lastName", person.getLastName());
  getResponse().addHeader("X-JSON", obj.toString());
  return null;

I’m just using the JSON library to convert a Java object to a JSON string. They really need to make that library a little better so that you don’t have to manually convert each property. Maybe use a bean introspector to inspect the Java object and create a JSONObject from there? Anyways, once that’s done, the JavaScript code is fairly straightforward:

function selectPerson(id) {
  new Ajax.Request('${base}/getPerson.action', {
    parameters: {'id' : id},
    onComplete: function(transport, json) {
      alert(json.firstName + ' ' + json.lastName);

From the Prototype documentation:

…if the response holds a X-JSON header, its content will be parsed, saved as an object and sent to the callbacks as the second argument…

Although you could decide to do an eval() or String#evalJSON() on the transport.responseText to retrieve the JSON object, you could just as easily use the second argument which Prototype provides for you.

I’m using Struts 2 here and notice that return value of the action is null. This is necessary because we don’t want to dispatch the user anywhere since this is an AJAXy call. The Struts action is defined without any results, from struts.xml:

<action name="getPerson"

Good to live in the world of Prototype.


8 thoughts on “Prototype + X-JSON + Struts 2

  1. arsenalist

    jsonplugin sounds nice and I could see me using it in some cases. But I’d be more interested in something which nicely converts Java objects to JSON strings without the manual labor of creating JSONObjects yourself. I know its not a hard thing to write and I should probably just do it instead of waiting for someone else.

    Something like this and perhaps more elegant.

    public PersonAction extends ActionSupport {
    private Person person;
    // getter setter for person

    public String execute() {
    // convert person to json, make it part of the
    // response, set the content type etc.
    return null;

  2. musachy

    But the JSON plugins creates the JSON string using introspection on your objects, which can be customized using annotations, plus other minor things, like supporting JSON RPC(example and doc):

    //I sound like I’m trying to sell this stuff, “if you buy the JSON plugin you get the XML one half the price” 🙂

  3. Galileo

    What happened to good oo and design? This method is a mess. Hack!

    public String getPerson() {
    Person person = personService.getPerson(id);
    JSONObject obj = new JSONObject();
    obj.put(“firstName”, person.getFirstName());
    obj.put(“lastName”, person.getLastName());
    getResponse().addHeader(“X-JSON”, obj.toString());
    return null;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s