Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Going Live with a Grails-Powered Blog

Once you have built a Grails project, how do you deploy it? Learn how to use Groovy builders, filters, and tags to deliver a Grails project. Along the way you'll delve into AJAX and deploy a Grails application in Tomcat.


advertisement
he DevX article Build Your First Grails Project: A Grails-Powered Blog showed how easy it is to create a purely functional web application using Grails. This article expands on the original source code from Build Your First Grails Project to get the blog application ready to go live. The process requires the following steps:
  1. Creating an RSS feed with Groovy Builders
  2. Securing the blog
  3. Using layouts for presentation
  4. Testing the application
  5. Tagging posts using AJAX
  6. Deploying the project as a war file

Getting Out There with RSS
The blog from Build Your First Grails Project allows users to create posts and leave comments on posts. Now it needs an RSS feed to tell people about updates to the blog. Groovy provides an extremely simple mechanism called builders for constructing node-based structures, such as XML. A Groovy builder is a class that intercepts closure calls on it and builds up a node tree internally that represents the calls that have been made on it.



For comparison, consider the options for constructing an RSS feed in Java:

  • Build up an XML-formatted string.
  • Use the XML DOM to construct the XML
  • Download an open source RSS library such as Rome.

The first option is error prone, as there is no inherent structure in the contents of a string. The second option is extremely verbose and memory intensive. All the elements need to be constructed as objects, have their content and attributes set, and finally have all their children connected. The third option (using an external library) works and it avoids the problems of the other two, but it adds another dependency to the project. Enter Groovy builders.

The code in Listing 1 enables you to implement an RSS feed with Groovy Builders. It uses the Grails support for rendering with the Groovy MarkupBuilder to construct an RSS feed from the existing blog posts.

Listing 1 created an action in the PostController called rss. The posts are loaded in descending date order and then the RSS feed is rendered by calling the render method and specifying the content type as XML and passing in a builder closure that constructs the XML response.

The problem with this approach is you are not able to add a link element, as controllers automatically have a link method made available for creating HTML links. To get round this, you need to use the Groovy MarkupBuilder directly from a separate class. This class must be created in the src directory at the same level as the grails-app directory (see Listing 2 for an example).

You then need to modify the render method in the rss action to be like this, which also makes the action much more concise:

render(contentType: "text/xml," text:new PostRssBuilder(request:request) .buildRss(posts))

When using the Groovy MarkupBuilder each of the closure definitions represent an XML element, while the arguments to the closure either define the content of the element (if the argument is a String) or the attributes of the element (if the argument is a Map). Listing 3 shows the XML that will be generated by the MarkupBuilder for this example.

The XML generated by the builder will create a root node called "rss" with the attribute version="2.0." The "rss" element will have a single child "channel" and the "channel" element will have a single title, link, description, and language element with an item element for each post. The Groovy each method is used to generate an item element for each post.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap