advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Download the Code!
Sidebar 1. The Difference Between Unit and Integration Tests
Partners & Affiliates
advertisement
advertisement
Average Rating: 1/5 | Rate this item | 1 user has rated this item.
 Print Print
 
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.

Page 1 of 5


advertisement
  Next Page: Securing The Blog
Page 1: IntroductionPage 4: Simple Tagging with AJAX
Page 2: Securing The BlogPage 5: Packaging and Deploying the Blog Application
Page 3: Making Yourself Presentable 
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES