Browse DevX
Sign up for e-mail newsletters from DevX


Going Live with a Grails-Powered Blog : Page 3

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Making Yourself Presentable
The next step in getting the application ready for launch is to think about making it more presentable—not that you would leave user interface considerations to this late stage on a normal project! So far the HTML is clean and minimal, but you need to think about common page elements and styling.

Grails uses SiteMesh as its rendering technology. The SiteMesh layouts are defined in the grails-app/views/layouts directory. You can apply the default Grails layout to the post/list.gsp file by using an HTML meta tag like this:

<meta name="layout" content="main">

This tells SiteMesh that main.gsp should be used as the layout for this page. Figure 4 shows the result of applying the default Grails layout.

Figure 4. Applying the Default Grails Layout: You can apply the default layout template that comes with Grails to the application.

To create your own layout, create the file blog.gsp under the grails-app/views/layouts folder with the HTML code shown here:

<%@ page contentType="text/html;charset=UTF-8" %> <html> <head> <title>Groovy Publish - <g:layoutTitle/></title> <g:layoutHead/> </head> <body> <g:layoutBody/> </body> </html>

When you apply this layout to all the GSPs, each page will be decorated with the contents of the layout. When a page with a layout is rendered, the layout will provide the main structure of the page and will pull in the contents of the page it is being used to render using the three available layout tags:

  • layoutTitle: pulls the contents of the HTML <title> tag from the rendered GSP into the layout
  • layoutHead: pulls the contents of the HTML <head> tag from the rendered GSP into the layout
  • layoutBody: pulls the contents of the <body> tag from the rendered GSP

Now that you have the layout defined, you can put the common page elements into it, such as the login and logout links:

<%@ page contentType="text/html;charset=UTF-8" %> <html> <head> <title>Groovy Publish - <g:layoutTitle/></title> <link rel="stylesheet" href="${createLinkTo(dir:'css',file:'screen.css')}" /> <g:layoutHead/> </head> <body> <div id="authlinks"> <gp:secureLink controller="user" action="logout"> Logout </gp:secureLink> <gp:noAuthOnlyLink controller="user" action="login"> Login </gp:noAuthOnlyLink> </div> <g:layoutBody/> </body> </html>

This updated layout shows a login link if the user is not authenticated and a logout link if the user is authenticated. Notice that there is a link to a CSS for styling. The createLinkTo method tells Grails to look in the css directory under the web-app directory. All static resources, such as CSS, images, and JavaScript files are stored under the web-app directory.

Testing Time
Grails provides two types of testing by default: unit and integration (see Sidebar 1. The Difference Between Unit and Integration Tests). If you create domain classes, controllers, and services through the Grails command line, you will automatically have integration tests created for these classes. Test classes can be found in the test directory at the same level as the grails-app directory. All unit and integration tests can be run from the command line by executing this command:

grails test-app

Listing 5 shows an integration test for the Post domain class showing the available Grails CRUD operations and also for ensuring that the constraints are working correctly.

Each test method is named explicitly to add clarity to the thing that is under test. To reduce the verbosity of this approach, a number of utility test methods (field*) have been created using Groovy meta programming. Each of these methods uses the setProperty method provided by the GroovyObject interface.

Thanks for your registration, follow us on our social networks to keep up-to-date