Tuesday, April 18, 2006

Grails: The Render method and Ajax

Users of Ruby on Rails will be familiar with the 'render' method which allows rendering of responses from a controller. Grails has a similar mechanism, for example rendering text is as simple as:


render 'hello world!'
or
render(text:'world',contentType:'text/xml')


You can of course also render templates and views via the method (for more complete documentation see here), but what makes Grails' render method that little bit different is its support for markup building.

With other Java frameworks I've used I ended up with loads of partial views implemented as JSP or velocity views. Each with the sole responsibility of rendering some XML or JSON. With Grails this is rarely needed for pure XML responses (note, I would never condone defining layout in this way this is purely for data oriented XML rendering). Rendering XML is made infinitely more simple:


def results = Book.list()
render(contentType:'text/xml') {
books {
for(b in results) {
book(title:b.title)
}
}
}


The above results in the following XML snippet being rendered to the response:


<books>
<book title="The Shining" />
<book title="Along came a Spider" />
</books>


When you combine this with how Grails controllers reload automatically on changing it makes writing ajax responses a breeze. But, there is more! Grails has inbuilt support for OpenRico so if you want to write Ajax responses in Ricos required format this can be done as follows:


render(builder:'rico') {
object(id:'bookUpdater') {
books {
for(b in results) {
book(title:b.title)
}
}
}
}


Rico will then automatically look-up your 'bookUpdater' instance and delegate the response to it passing the contained XML to the updater for handling.

I don't normally like talking about unimplemented features, but thought it might be worthwhile just to highlight the power of the builder concept in Groovy as I don't believe that many grasp the potential of builders and their usages. One of the upcoming features in Grails is a JSON builder which will be available via the render method so say I wanted to render JSON instead of XML? We just change the content type defined in the render method:


render(contentType:'text/json') {
books {
for(b in results) {
book(title:b.title)
}
}
}


And the resulting output would be JSON instead of the XML without any change to the structure of the builder code itself:


{
books : [
{ title: 'The Shining' },
{ title: 'Along came a spider' }
]
}

6 comments:

Anonymous said...

its xml, so you must close all tags:
<book ... />

Philippe said...

Hi,
how can i update value of an input type="text" with ajax and a controller method ?

i've tried with the rico builder but i've not the good synthax

thanks

derfefww said...

It is the Archlord gold which make me very happy these days, my brother says Archlord money is his favorite games gold he likes, he usually buy some archlord online Gold to start his game and most of the time he will win the buy Archlord gold back and give me some cheap Archlord gold to play the game.

Anonymous said...

In my round, in spare time many people said that the maple story is very interesting, many people said that in the game how to earn more maple story mesos, and how to spend a little money then get more ms mesos, so with them discuss, I beginning to contact the game, first I need to buy maple story mesos, because I do not play must had more maple mesos, so I need to had enough money.

cheap wow power leveling said...

Buy Warcraft(wow power leveling)please point machine (wow power leveling)to enter the website(wow power leveling)of Warcraft

Anonymous said...

^^ nice blog!! ^@^

徵信, 徵信網, 徵信社, 徵信社, 感情挽回, 婚姻挽回, 挽回婚姻, 挽回感情, 徵信, 徵信社, 徵信, 徵信, 捉姦, 徵信公司, 通姦, 通姦罪, 抓姦, 抓猴, 捉猴, 捉姦, 監聽, 調查跟蹤, 反跟蹤, 外遇問題, 徵信, 捉姦, 女人徵信, 女子徵信, 外遇問題, 女子徵信, 外遇, 徵信公司, 徵信網, 外遇蒐證, 抓姦, 抓猴, 捉猴, 調查跟蹤, 反跟蹤, 感情挽回, 挽回感情, 婚姻挽回, 挽回婚姻, 外遇沖開, 抓姦, 女子徵信, 外遇蒐證, 外遇, 通姦, 通姦罪, 贍養費, 徵信, 徵信社, 抓姦, 徵信, 徵信公司, 徵信社, 徵信公司, 徵信社, 徵信公司, 女人徵信,

徵信, 徵信網, 徵信社, 徵信網, 外遇, 徵信, 徵信社, 抓姦, 徵信, 女人徵信, 徵信社, 女人徵信社, 外遇, 抓姦, 徵信公司, 徵信社, 徵信社, 徵信社, 徵信社, 徵信社, 女人徵信社, 徵信社, 徵信, 徵信社, 徵信, 女子徵信社, 女子徵信社, 女子徵信社, 女子徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信社,

徵信, 徵信社,徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 外遇, 抓姦, 離婚, 外遇,離婚,

徵信社,徵信, 徵信社, 徵信, 徵信社, 徵信,徵信社, 徵信社, 徵信, 外遇, 抓姦, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信社, 徵信社, 徵信社,徵信,徵信, 徵信, 外遇, 抓姦