<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Oliver's Place &#187; Groovy</title>
	<atom:link href="http://weichhold.com/category/groovy/feed/" rel="self" type="application/rss+xml" />
	<link>http://weichhold.com</link>
	<description></description>
	<lastBuildDate>Fri, 10 Sep 2010 08:21:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>A simple GSP toolbar taglib for Grails</title>
		<link>http://weichhold.com/2010/01/13/a-simple-gsp-toolbar-taglib-for-grails/</link>
		<comments>http://weichhold.com/2010/01/13/a-simple-gsp-toolbar-taglib-for-grails/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 15:55:26 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://weichhold.com/?p=112</guid>
		<description><![CDATA[Did you ever had to implement a menu in Grails GSP where every link element should be seperated from its predecessor by a separator character and where some of the elements can be missing when certain conditions are not met (security etc)? The part with the optional elements can turn the separator handling into a [...]]]></description>
			<content:encoded><![CDATA[<p>Did you ever had to implement a menu in Grails GSP where every link element should be seperated from its predecessor by a separator character and where some of the elements can be missing when certain conditions are not met (security etc)?</p>
<p>The part with the optional elements can turn the separator handling into a real mess. So today I decided to end this nonsense. The result is a tiny taglib which can be integrated into every grails project. The usage is pretty self explanatory:</p>
<p><b><br />
&lt;mx:toolbar separator=&#8217;|'&gt;<br />
&nbsp;&nbsp;&lt;mx:item&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;shiro:hasPermission permission=&#8221;topic:delete&#8221;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;g:link controller=&#8221;topic&#8221; action=&#8221;delete&#8221; id=&#8221;${topic.id}&#8221;&gt;&lt;g:message code=&#8221;topic.delete&#8221;&gt;&lt;/g:message&gt;&lt;/g:link&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/shiro:hasPermission&gt;<br />
&nbsp;&nbsp;&lt;/mx:item&gt;</p>
<p>&nbsp;&nbsp;&lt;mx:item&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;shiro:hasPermission permission=&#8221;topic:lock&#8221;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;g:link controller=&#8221;topic&#8221; action=&#8221;lock&#8221; id=&#8221;${topic.id}&#8221;&gt;&lt;g:message code=&#8221;topic.lock&#8221;&gt;&lt;/g:message&gt;&lt;/g:link&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/shiro:hasPermission&gt;<br />
&nbsp;&nbsp;&lt;/mx:item&gt;<br />
&lt;/mx:toolbar&gt;<br />
</b></p>
<p><span id="more-112"></span></p>
<p>And this would be rendered assuming the user has permission for both buttons:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p112code3'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1123"><td class="code" id="p112code3"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;/topic/delete/10&quot;&gt;Delete&lt;/a&gt;|&lt;a href=&quot;/topic/lock/10&quot;&gt;Lock&lt;/a&gt;</pre></td></tr></table></div>

<p>Tag Library Source:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p112code4'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1124"><td class="code" id="p112code4"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MenuTagLib
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">static</span> namespace <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'mx'</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/** The toolbar container */</span>
  <span style="color: #000000; font-weight: bold;">def</span> toolbar <span style="color: #66cc66;">=</span>
  <span style="color: #66cc66;">&#123;</span> attrs, body <span style="color: #66cc66;">-&gt;</span>
    <span style="color: #000000; font-weight: bold;">def</span> separator <span style="color: #66cc66;">=</span> attrs.<span style="color: #006600;">separator</span> <span style="color: #66cc66;">?</span>: <span style="color: #ff0000;">'|'</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// we'll be using this to keep track of our items</span>
    <span style="color: #000000; font-weight: bold;">def</span> menuItems <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">pageScope</span>.<span style="color: #006600;">menuItems</span> <span style="color: #66cc66;">=</span> menuItems
&nbsp;
    <span style="color: #808080; font-style: italic;">// this will execute the body, so that the items are appended obviously,</span>
    <span style="color: #808080; font-style: italic;">// anything other than the item tags will not render correctly.</span>
    body<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// render every item and add a separator except for the last item </span>
    menuItems.<span style="color: #663399;">eachWithIndex</span>
    <span style="color: #66cc66;">&#123;</span> item, index <span style="color: #66cc66;">-&gt;</span>
      out <span style="color: #66cc66;">&lt;&lt;</span> item
      <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>index <span style="color: #66cc66;">&lt;</span> menuItems.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        out <span style="color: #66cc66;">&lt;&lt;</span> separator
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/** A toolbar item */</span>
  <span style="color: #000000; font-weight: bold;">def</span> item <span style="color: #66cc66;">=</span>
  <span style="color: #66cc66;">&#123;</span> attrs, body <span style="color: #66cc66;">-&gt;</span>
    <span style="color: #000000; font-weight: bold;">def</span> itemContent <span style="color: #66cc66;">=</span> body<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">trim</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>itemContent<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// only add the item if rendering evaluated to non-empty string</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">pageScope</span>.<span style="color: #006600;">menuItems</span> <span style="color: #66cc66;">&lt;&lt;</span> itemContent
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<img src="http://weichhold.com/?ak_action=api_record_view&id=112&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weichhold.com/2010/01/13/a-simple-gsp-toolbar-taglib-for-grails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grails sanitized stracktraces</title>
		<link>http://weichhold.com/2009/10/10/grails-sanitized-stracktraces/</link>
		<comments>http://weichhold.com/2009/10/10/grails-sanitized-stracktraces/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 21:01:35 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://weichhold.com/?p=107</guid>
		<description><![CDATA[The other day I was writing a medium complex HQL query for a Grails App and no matter what the line executing the query would crash like this: ?View Code GROOVYjava.lang.NullPointerException at $Proxy13.createQuery&#40;Unknown Source&#41; at com.acme.PerformanceService.makePerformanceSheet&#40;PerformanceService.groovy:38&#41; ... So how the hell do we figure out what the problem given so few information? Turns out that [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I was writing a medium complex HQL query for a Grails App and no matter what the line executing the query would crash like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p107code8'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1078"><td class="code" id="p107code8"><pre class="groovy" style="font-family:monospace;">java.<span style="color: #006600;">lang</span>.<span style="color: #aaaadd; font-weight: bold;">NullPointerException</span>
	at $Proxy13.<span style="color: #006600;">createQuery</span><span style="color: #66cc66;">&#40;</span>Unknown Source<span style="color: #66cc66;">&#41;</span>
	at com.<span style="color: #006600;">acme</span>.<span style="color: #006600;">PerformanceService</span>.<span style="color: #006600;">makePerformanceSheet</span><span style="color: #66cc66;">&#40;</span>PerformanceService.<span style="color: #006600;">groovy</span>:<span style="color: #cc66cc;">38</span><span style="color: #66cc66;">&#41;</span>
  ...</pre></td></tr></table></div>

<p>So how the hell do we figure out what the problem given so few information? Turns out that grails sanitizes the exception stacktrace, resulting in better readability by removing a lot of clutter. Sometimes though this optimization will also hide some important details from you. Luckily we can still refer to stacktrace.log which usually resides in the project root folder in development mode. In this particular case opening the file reveiled this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p107code9'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1079"><td class="code" id="p107code9"><pre class="groovy" style="font-family:monospace;">java.<span style="color: #006600;">lang</span>.<span style="color: #aaaadd; font-weight: bold;">NullPointerException</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">dialect</span>.<span style="color: #006600;">Dialect</span>$<span style="color: #cc66cc;">3</span>.<span style="color: #006600;">getReturnType</span><span style="color: #66cc66;">&#40;</span>Dialect.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">125</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">util</span>.<span style="color: #006600;">SessionFactoryHelper</span>.<span style="color: #006600;">findFunctionReturnType</span><span style="color: #66cc66;">&#40;</span>SessionFactoryHelper.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">405</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">tree</span>.<span style="color: #006600;">AggregateNode</span>.<span style="color: #006600;">getDataType</span><span style="color: #66cc66;">&#40;</span>AggregateNode.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">44</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">tree</span>.<span style="color: #006600;">SelectClause</span>.<span style="color: #006600;">initializeExplicitSelectClause</span><span style="color: #66cc66;">&#40;</span>SelectClause.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">165</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">HqlSqlWalker</span>.<span style="color: #006600;">useSelectClause</span><span style="color: #66cc66;">&#40;</span>HqlSqlWalker.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">727</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">HqlSqlWalker</span>.<span style="color: #006600;">processQuery</span><span style="color: #66cc66;">&#40;</span>HqlSqlWalker.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">551</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">antlr</span>.<span style="color: #006600;">HqlSqlBaseWalker</span>.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span>HqlSqlBaseWalker.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">645</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">antlr</span>.<span style="color: #006600;">HqlSqlBaseWalker</span>.<span style="color: #006600;">selectStatement</span><span style="color: #66cc66;">&#40;</span>HqlSqlBaseWalker.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">281</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">antlr</span>.<span style="color: #006600;">HqlSqlBaseWalker</span>.<span style="color: #006600;">statement</span><span style="color: #66cc66;">&#40;</span>HqlSqlBaseWalker.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">229</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">QueryTranslatorImpl</span>.<span style="color: #006600;">analyze</span><span style="color: #66cc66;">&#40;</span>QueryTranslatorImpl.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">251</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">QueryTranslatorImpl</span>.<span style="color: #006600;">doCompile</span><span style="color: #66cc66;">&#40;</span>QueryTranslatorImpl.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">183</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">QueryTranslatorImpl</span>.<span style="color: #006600;">compile</span><span style="color: #66cc66;">&#40;</span>QueryTranslatorImpl.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">134</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">engine</span>.<span style="color: #006600;">query</span>.<span style="color: #006600;">HQLQueryPlan</span>.<span style="color: #66cc66;">&lt;</span>init<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span>HQLQueryPlan.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">101</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">engine</span>.<span style="color: #006600;">query</span>.<span style="color: #006600;">HQLQueryPlan</span>.<span style="color: #66cc66;">&lt;</span>init<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span>HQLQueryPlan.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">80</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">engine</span>.<span style="color: #006600;">query</span>.<span style="color: #006600;">QueryPlanCache</span>.<span style="color: #006600;">getHQLQueryPlan</span><span style="color: #66cc66;">&#40;</span>QueryPlanCache.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">94</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">impl</span>.<span style="color: #006600;">AbstractSessionImpl</span>.<span style="color: #006600;">getHQLQueryPlan</span><span style="color: #66cc66;">&#40;</span>AbstractSessionImpl.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">156</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">impl</span>.<span style="color: #006600;">AbstractSessionImpl</span>.<span style="color: #006600;">createQuery</span><span style="color: #66cc66;">&#40;</span>AbstractSessionImpl.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">135</span><span style="color: #66cc66;">&#41;</span>
  ...</pre></td></tr></table></div>

<p>Which almost instantly gave me the hint that something&#8217;s wrong with the aggregate part of my query:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p107code10'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10710"><td class="code" id="p107code10"><pre class="groovy" style="font-family:monospace;">org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">tree</span>.<span style="color: #006600;">AggregateNode</span>.<span style="color: #006600;">getDataType</span></pre></td></tr></table></div>

<p>And indeed the problem was that I had ported an SQL Query to HQL and forgot to rename the sum() aggregate argument from the column name to the domain class property name:</p>
<p>sum(some_property) instead of sum(foo.someProperty)</p>
<img src="http://weichhold.com/?ak_action=api_record_view&id=107&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weichhold.com/2009/10/10/grails-sanitized-stracktraces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Updating multiple page elements with Grails and Ajax</title>
		<link>http://weichhold.com/2009/07/20/updating-multiple-page-fragments-with-grails-and-ajax/</link>
		<comments>http://weichhold.com/2009/07/20/updating-multiple-page-fragments-with-grails-and-ajax/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 09:30:01 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://weichhold.com/?p=39</guid>
		<description><![CDATA[Every Web Application Project I’ve been involved in the recent past had one thing in common: the client demanded more and more rich-client features at an ever increasing pace &#8211; without committing itself to real rich-client frameworks such as Flex or Silverlight. This leaves us with Javascript / AJAX for implementing highly dynamic User Interfaces [...]]]></description>
			<content:encoded><![CDATA[<p>Every Web Application Project I’ve been involved in the recent past had one thing in common: the client demanded more and more rich-client features at an ever increasing pace &#8211; without committing itself to real rich-client frameworks such as Flex or Silverlight. This leaves us with Javascript / AJAX for implementing highly dynamic User Interfaces for Web Applications. A very common problem when working with Ajax is the requirement to update multiple parts of a page in response to a single request. This post aims to familiarize the reader with my own approach to this problem.</p>
<p>At its core my solution consists of just two functions &#8211; one running server side and the other executing on the client. The first function is intended to run on the server and is written in Groovy. It was originally written in Python for use with the <a href="http://djangoproject.com">Django</a> framework and I&#8217;ve ported it to Groovy when I switched to <a href="http://grails.org">Grails</a> as my primary RAD Web framework. This function takes a map as argument and returns a JSON encoded map where the key is a valid <a href="http://docs.jquery.com/Selectors">jQuery Selector </a> designating the targets for the update and the value is a map containing information how the matched elements should be modified and how the content for the update shall be produced. Don&#8217;t worry if you find this confusing at first. There&#8217;s a sample further down.</p>
<p><span id="more-39"></span></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code20'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3920"><td class="code" id="p39code20"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> renderElementUpdateResponse<span style="color: #66cc66;">&#40;</span>elements<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">def</span> rendered_elements <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>:<span style="color: #66cc66;">&#93;</span>
&nbsp;
  elements.<span style="color: #663399;">eachWithIndex</span>
  <span style="color: #66cc66;">&#123;</span> k, v, i <span style="color: #66cc66;">-&gt;</span>
    rendered_elements<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>:<span style="color: #66cc66;">&#93;</span>
    rendered_elements<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'mode'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> v<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'mode'</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// if content is a Map, feed it into g.render - using the key as template name and the value as model</span>
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>v<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'content'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> java.<span style="color: #006600;">util</span>.<span style="color: #006600;">LinkedHashMap</span><span style="color: #66cc66;">&#41;</span>
      rendered_elements<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'content'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> g.<span style="color: #006600;">render</span><span style="color: #66cc66;">&#40;</span>template: v<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'content'</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">keySet</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>, model: v<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'content'</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">values</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">else</span>
      rendered_elements<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'content'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> v<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'content'</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">def</span> tmp <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'updates'</span>: rendered_elements<span style="color: #66cc66;">&#93;</span>
  render tmp <span style="color: #000000; font-weight: bold;">as</span> JSON
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The second function is written in Javascript and responsible for applying the JSON wrapped HTML fragments to their designated HTML DOM Elements:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code21'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3921"><td class="code" id="p39code21"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> applyElementUpdates<span style="color: #009900;">&#40;</span>transport<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> updates<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>transport.<span style="color: #660066;">updates</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    updates <span style="color: #339933;">=</span> transport.<span style="color: #660066;">updates</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> scripts <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>  <span style="color: #006600; font-style: italic;">// need to queue initializers until all updates are done</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>target <span style="color: #000066; font-weight: bold;">in</span> updates<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>updates<span style="color: #009900;">&#91;</span>target<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'mode'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'execute'</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        code <span style="color: #339933;">=</span> updates<span style="color: #009900;">&#91;</span>target<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#93;</span>
        scripts.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>code<span style="color: #009900;">&#41;</span>
        <span style="color: #000066; font-weight: bold;">continue</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #003366; font-weight: bold;">var</span> targetNode <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> newContent <span style="color: #339933;">=</span> updates<span style="color: #009900;">&#91;</span>target<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> oldContent <span style="color: #339933;">=</span> targetNode.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>updates<span style="color: #009900;">&#91;</span>target<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'mode'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'replace'</span><span style="color: #339933;">:</span>
          targetNode.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>newContent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'insert'</span><span style="color: #339933;">:</span>
          targetNode.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>newContent <span style="color: #339933;">+</span> oldContent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'append'</span><span style="color: #339933;">:</span>
          targetNode.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>oldContent <span style="color: #339933;">+</span> newContent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// now run initializers</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #000066; font-weight: bold;">in</span> scripts<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      code <span style="color: #339933;">=</span> scripts<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>
      <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span>code<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So how do you use it? Below you find all necessary code artifacts for implementing the described technique in arbitrary Grails application. Please note that this sample uses <a href="jquery.com">jQuery</a> for carrying out the actual AJAX request but it would be trivial to port this over to any other decent Javascript Framework like <a href="mootools.net">MooTools</a>, Prototype or YUI.<br />
<br/><br />
Let&#8217;s begin with the GSP page in <b>/views/test/index.gsp</b>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code22'); return false;">View Code</a> ASP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3922"><td class="code" id="p39code22"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">@</span> page <span style="color: #330066;">contentType</span><span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;text/html;charset=UTF-8&quot;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;</span>html<span style="color: #006600; font-weight: bold;">&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;</span>head<span style="color: #006600; font-weight: bold;">&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>g<span style="color: #006600; font-weight: bold;">:</span>javascript src<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;jquery-1.3.2.js&quot;</span> <span style="color: #006600; font-weight: bold;">/&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>g<span style="color: #006600; font-weight: bold;">:</span>javascript src<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;applyElementUpdates.js&quot;</span> <span style="color: #006600; font-weight: bold;">/&gt;</span>
&nbsp;
    <span style="color: #006600; font-weight: bold;">&lt;</span>script type<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;text/javascript&quot;</span><span style="color: #006600; font-weight: bold;">&gt;</span>
    $<span style="color: #006600; font-weight:bold;">&#40;</span>document<span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">ready</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #0000ff; font-weight: bold;">function</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #006600; font-weight:bold;">&#123;</span>
      $<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #008000;">'#btnClickMe').click(function()</span>
      <span style="color: #006600; font-weight:bold;">&#123;</span>
        $.<span style="color: #9900cc;">ajax</span><span style="color: #006600; font-weight:bold;">&#40;</span>
        <span style="color: #006600; font-weight:bold;">&#123;</span>
          url<span style="color: #006600; font-weight: bold;">:</span> <span style="color: #008000;">'${createLink(controller: 'test', action: 'update')}',</span>
          type<span style="color: #006600; font-weight: bold;">:</span> <span style="color: #cc0000;">&quot;POST&quot;</span>,
          dataType<span style="color: #006600; font-weight: bold;">:</span> <span style="color: #cc0000;">&quot;json&quot;</span>,
          cache<span style="color: #006600; font-weight: bold;">:</span> <span style="color: #0000ff; font-weight: bold;">false</span>,
          async<span style="color: #006600; font-weight: bold;">:</span> <span style="color: #0000ff; font-weight: bold;">true</span>,
          success<span style="color: #006600; font-weight: bold;">:</span> <span style="color: #0000ff; font-weight: bold;">function</span><span style="color: #006600; font-weight:bold;">&#40;</span>data, <span style="color: #330066;">status</span><span style="color: #006600; font-weight:bold;">&#41;</span>
          <span style="color: #006600; font-weight:bold;">&#123;</span>
            applyElementUpdates<span style="color: #006600; font-weight:bold;">&#40;</span>data<span style="color: #006600; font-weight:bold;">&#41;</span>
          <span style="color: #006600; font-weight:bold;">&#125;</span>
        <span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
      <span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
    <span style="color: #006600; font-weight: bold;">&lt;/</span>script<span style="color: #006600; font-weight: bold;">&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;/</span>head<span style="color: #006600; font-weight: bold;">&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;</span>body<span style="color: #006600; font-weight: bold;">&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;</span>input type<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;button&quot;</span> id<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;btnClickMe&quot;</span> value<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Click me&quot;</span><span style="color: #006600; font-weight: bold;">/&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;</span>br<span style="color: #006600; font-weight: bold;">/&gt;&lt;</span>br<span style="color: #006600; font-weight: bold;">/&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;</span>div id<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;div1&quot;</span><span style="color: #006600; font-weight: bold;">&gt;&lt;/</span>div<span style="color: #006600; font-weight: bold;">&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;</span>div id<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;div2&quot;</span><span style="color: #006600; font-weight: bold;">&gt;&lt;/</span>div<span style="color: #006600; font-weight: bold;">&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;/</span>body<span style="color: #006600; font-weight: bold;">&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;/</span>html<span style="color: #006600; font-weight: bold;">&gt;</span></pre></td></tr></table></div>

<p>The controller in <b>/controllers/TestController.groovy</b>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code23'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3923"><td class="code" id="p39code23"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">grails.converters.JSON</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> TestController <span style="color: #000000; font-weight: bold;">extends</span> BaseController
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">def</span> index <span style="color: #66cc66;">=</span>
  <span style="color: #66cc66;">&#123;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">def</span> update <span style="color: #66cc66;">=</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> model <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>a: <span style="color: #cc66cc;">1</span>, b: <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// Note: renderElementUpdateResponse is supposed to be implemented in BaseController</span>
    <span style="color: #000000; font-weight: bold;">return</span> renderElementUpdateResponse<span style="color: #66cc66;">&#40;</span>
    <span style="color: #66cc66;">&#91;</span>
      <span style="color: #ff0000;">'#div1'</span>   : <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'mode'</span>: <span style="color: #ff0000;">'replace'</span>, <span style="color: #ff0000;">'content'</span> : <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'fragment1'</span>: model <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#93;</span>,
      <span style="color: #ff0000;">'#div2'</span>   : <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'mode'</span>: <span style="color: #ff0000;">'append'</span>, <span style="color: #ff0000;">'content'</span>  : <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'fragment2'</span>: model <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#93;</span>,
      <span style="color: #ff0000;">'__init__'</span>: <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'mode'</span>: <span style="color: #ff0000;">'execute'</span>, <span style="color: #ff0000;">'content'</span> : <span style="color: #ff0000;">'alert(&quot;The page has been updated&quot;)'</span> <span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The call to renderElementUpdateResponse needs some explanation. Let&#8217;s step through the arguments line by line.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code24'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3924"><td class="code" id="p39code24"><pre class="groovy" style="font-family:monospace;"> <span style="color: #ff0000;">'#div1'</span>: <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'mode'</span>: <span style="color: #ff0000;">'replace'</span>, <span style="color: #ff0000;">'content'</span> : <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'fragment1'</span>: model <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#93;</span></pre></td></tr></table></div>

<p><i>&#8220;[ 'fragment1': model ] ]&#8221;</i> is the only relevant part on the server side. It means &#8220;render the GSP template /views/test/_fragment1.gsp, pass it the variable model as the model and store the resulting string as the content for the update. On the client side it tells the applyElementUpdates() function to replace <i>(mode: replace)</i> the output for this line in all elements with id=&#8221;div1&#8243;.<br />
<br/></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code25'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3925"><td class="code" id="p39code25"><pre class="groovy" style="font-family:monospace;"> <span style="color: #ff0000;">'#div2'</span>: <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'mode'</span>: <span style="color: #ff0000;">'append'</span>, <span style="color: #ff0000;">'content'</span> : <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'fragment2'</span>: model <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#93;</span></pre></td></tr></table></div>

<p>Works basically the same as the previous line only that id=&#8221;div2&#8243; is targetted and rendering /views/test/_fragment2.gsp will produce the content which is going to be appended to the target element rather than replacing it.<br />
<br/></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code26'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3926"><td class="code" id="p39code26"><pre class="groovy" style="font-family:monospace;"> <span style="color: #ff0000;">'__init__'</span>: <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'mode'</span>: <span style="color: #ff0000;">'execute'</span>, <span style="color: #ff0000;">'content'</span> : <span style="color: #ff0000;">'alert(&quot;The page has been updated&quot;)'</span> <span style="color: #66cc66;">&#93;</span></pre></td></tr></table></div>

<p>In addition to updating DOM elements we can also send code to the client which is run <b>after</b> all elements have been updated. Such fragments are called initializers and are identified on the client by their mode attribute which must be &#8216;execute&#8217;. This sample also demonstrates that fragment content can be specifed directly (<i>alert(&#8220;The page has been updated&#8221;)</i>) instead of being produced by rendering a template. Please note that the selector (&#8216;__init__&#8217;) is unused in this case. The sole reason for chosing that id was to emphasize the purpose of this fragment.<br />
<br/></p>
<p>The final missing bits are the aforementioned Grails templates which are going to producing the content for the updated elements. </p>
<p>The fragment for updating the first div in <b>/views/test/_fragment1.gsp</b>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code27'); return false;">View Code</a> ASP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3927"><td class="code" id="p39code27"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span>span<span style="color: #006600; font-weight: bold;">&gt;</span>Value of A <span style="color: #990099; font-weight: bold;">is</span> $<span style="color: #006600; font-weight:bold;">&#123;</span>a<span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight: bold;">&lt;/</span>span<span style="color: #006600; font-weight: bold;">&gt;</span></pre></td></tr></table></div>

<p>The fragment for updating the second div in <b>/views/test/_fragment2.gsp</b>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code28'); return false;">View Code</a> ASP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3928"><td class="code" id="p39code28"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span>span<span style="color: #006600; font-weight: bold;">&gt;</span>Value of B <span style="color: #990099; font-weight: bold;">is</span> $<span style="color: #006600; font-weight:bold;">&#123;</span>b<span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight: bold;">&lt;/</span>span<span style="color: #006600; font-weight: bold;">&gt;&lt;</span>br<span style="color: #006600; font-weight: bold;">/&gt;</span></pre></td></tr></table></div>

<p>Finally, opening the example page on your browser and clicking the button results in the following actions:</p>
<ul>
<li>The div1 element gets updated with the HTML output of rendering /views/test/_fragment1.gsp against the simple model. The contents of div1 are replaced</li>
<li>The div2 element gets updated with the HTML output of rendering /views/test/_fragment2.gsp against the simple model. The HTML is appended to the div rather then replacing the contents.</li>
<li>An alert box is displayed</li>
</ul>
<p>That&#8217;s it folks. I hope you found this post useful. </p>
<img src="http://weichhold.com/?ak_action=api_record_view&id=39&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weichhold.com/2009/07/20/updating-multiple-page-fragments-with-grails-and-ajax/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Using GIT with Grails UI-Performance Plugin</title>
		<link>http://weichhold.com/2009/06/18/using-git-with-grails-ui-performance-plugin/</link>
		<comments>http://weichhold.com/2009/06/18/using-git-with-grails-ui-performance-plugin/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 18:16:53 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://weichhold.com/?p=36</guid>
		<description><![CDATA[I&#8217;ve been doing a serious amount of Grails Development lately and if there&#8217;s one Grails Plugin I consider mandatory for any serious production deployment then it is the UI-Performance plugin by Burt Beckwith. One of the features of the plugin that reduces server load is versioning and far-future &#8220;Expires&#8221; and &#8220;Cache-Control&#8221; headers for static resources. Each time [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing a serious amount of Grails Development lately and if there&#8217;s one Grails Plugin I consider mandatory for any serious production deployment then it is the <a href="http://grails.org/plugin/ui-performance" target="_blank">UI-Performance plugin</a> by Burt Beckwith. One of the features of the plugin that reduces server load is versioning and far-future &#8220;Expires&#8221; and &#8220;Cache-Control&#8221; headers for static resources.</p>
<p>Each time you generate a new WAR for deployment, a new version is applied to all image, .js, and .css files so files can be cached forever. The next time you deploy a new version of your application, all of the resource file names will have changed, so only then will files be requested. The default process for determining the version is to look at the version of the root project folder in the Subversion metadata files. Since I&#8217;ve switched to <a href="http://git-scm.com/">GIT</a> for all my projects a while ago the plugin would fallback to <em>&#8220;System.currentTimeMillis().toString()&#8221; </em>to compute the version.</p>
<p>My solution is to include the following line in my projects Config.groovy:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p36code30'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3630"><td class="code" id="p36code30"><pre class="groovy" style="font-family:monospace;">uiperformance.<span style="color: #006600;">determineVersion</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">-&gt;</span> <span style="color: #ff0000;">&quot;git rev-list --max-count=1 HEAD&quot;</span>.<span style="color: #993399; font-weight: bold;">execute</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">text</span>.<span style="color: #006600;">trim</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This allows the HEAD commit SHA1 hash of the current branch to be used as the uiperformance version for deployment.</p>
<img src="http://weichhold.com/?ak_action=api_record_view&id=36&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weichhold.com/2009/06/18/using-git-with-grails-ui-performance-plugin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
