Tech Note: Scaffolding JSON with Grails
{ Dan Stieglitz // Groovy/Grails // April 02, 2008 }
For the uninitiated, JSON “is a lightweight data-interchange format,” as humbly posted on JSON.org. It’s useful for architects in search of a lighter-than-XML transport or JavaScript developers who want service providers sending them easy to use objects for use in their pages. Grails has out-of-the-box JSON support via the JSONObject library, as well as through their converters “plugin” (which is now included in the core distribution). Converting grails domain objects to JSON (or XML, for the enthusiast) is as complicated as
render myDomainObject as JSON
and
render myDomainObject as XML
This is fantastic, and to include a JSON service in your app, you would simply add a method to your controller as such:
def json = {
if(!params.max) params.max = 10
render MyDomainObject.list( params ) as JSON
}
It doesn’t get better for the lazy developer, unless you want Grails to automatically add this method to each scaffolded controller class you generate. This is possible by customizing Grails’ scaffolding templates. First, install the templates by issuing
grails install-templates
at your project’s root. This will create the src/templates directory where the artifact and scaffold templates live. Crack open the src/templates/scaffolding/Controller.groovy file and add the following closure template:
def json = {
if(!params.max) params.max = 10
render ${className}.list( params ) as JSON
}
It’s essentially a copy of the list closure, except the controller will render a JSON string instead of passing a list to the view. You’ll also need to import the JSON converter so add this to the import section of the template:
import grails.converters.JSON;
That’s it, and the next time you generate a controller for one of your domain objects you can get a JSON list by hitting the /MyApp/MyDomainObject/json URL.
@see http://www.grails.org/Artifact+and+Scaffolding+Templates
@see http://www.json.org/
8 comments
hello friends !!! its a well written post and being a professional i really like this,, actually i was searching this kind of 642-164 exam information and now found it here on this nice site you have done a nice work and i also checked the link its very useful.. these days i am busy in my professional 642-241 exam classes and will come after that for more discussion..
Posted by
642-145 exam on 09/28 at 05:19 AM
I don`t know if God exists, but it would be better for his reputation if he didn`t. music ringtones
Posted by on 11/13 at 03:58 PM
I hope this is an interesting little insight into the way I develop concepts when working on a new design. Feel free to let me know what you think, and whether you found it..
http://www.uk888.com/
Posted by
blackjack internet strategies on 11/28 at 05:33 AM
Some people don’t know the correct way to finish the psychology essay paper. Thence, at this moment they could follow your fantastic story connected with this good post and just do the highest quality homework help ever.
Posted by
AVAFrye on 03/31 at 09:19 PM
It’s essentially a copy of the list closure, except the controller will render a JSON string instead of passing a list to the view. You’ll also need to import the JSON converter so add this to the import section of the template:
Posted by
square peg web on 06/30 at 11:10 PM
Every female in the our world wants to become unique, but doesn’t get know how to do that. But billions of different people find the mp3 ringtones or just mp ringtones to be original.
Posted by
Houston18Phyllis on 07/14 at 09:38 AM
thanks by admin perfect blog
Online olarak Film İzle
En güzel Dantel ler
hobi elbecerileri ve Dantel Dünyası
Birbirinden güzel Dantel örnekleri
En yeni Dantel modelleri
bedava Film seyret ve
bitkisel tedavi yöntemleri Şifalı bitkiler sitesini ziyaret edebilirsiniz.
Posted by
dantel on 07/22 at 12:55 PM
supply in stock and custom lace front wigs, full lace wigs, lace wigs, human hair wigs, remy lace front wigs, cheap wigs, cheap, buy, celebrity
lace wigs
Posted by
cosplay on 08/25 at 01:19 AM
Add your own comment below.
<< Back to main