Sunday, April 29, 2007

JSON enabling objects

So, MLTA is progressing nicely. In order to add to the "Wow" factor and maybe get some extra publicity once it launches, I've decided to fully AJAX enable it. The project is using Struts2, Spring and Hibernate (using JPA), and I've worked out a nice way of enabling AJAX calls via standard Struts2. What I've done is implement the JSONString interface, which basically defines a toJSONString method. You then build up the object using JSONObject(s). Any time you then want to retieve an object you simply call the standard find method and instead of returning a full HTML page, you simply use a JSP to output the value of toJSONString. Save's work out of the box, you just need to return a different vslue, for example 'success' for success, etc. Easy :)

Struts2 (formerly WebWork) includes some nice AJAX features out-of-the-box. One particularly nice feature is AJAX Tabs. However, one gotcha I came across was that you need all your Javascript outside of the Tabs. So, even for pages within a tab, the Javascript needs to be in the page hosting the tabs.

No comments: