Search

Dark theme | Light theme
Showing posts with label GSP. Show all posts
Showing posts with label GSP. Show all posts

February 25, 2011

Grails Goodness: Controller Properties as Model

To pass data from a controller to a view we must return a model. The model is a map with all the values we want to show on the Groovy Server Page (GSP). We can explicitly return a model from an action, but if we don't do that the controller's properties are passed as the model to the view. Remember that in Grails a new controller instance is created for each request. So it is save to use the properties of the controller as model in our views.

// File: grails-app/controllers/com/mrhaki/SampleController.groovy
package com.mrhaki

class SampleController {

    def values = ['Grails', 'Groovy', 'Griffon', 'Gradle', 'Spock']

    def greeting

    def index = {
        greeting = 'Welcome to My Blog'
        // Don't return a model, so the properties become the model.
    }
}
<%-- File: grails-app/views/sample/index.gsp --%>
<html>
    <head>
    </head>
    <body>
        <h1>${greeting}</h1>

        <g:join in="${values}"/> rock!
    </body>
</html>

February 24, 2011

Grails Goodness: Set Application Wide Default Layout

Grails uses Sitemesh as the layout and decoration framework. Layouts are defined in the grails-app/views/layouts directory. There are several conventions Grails uses to determine which layout file must be applied. We can set a default layout for our application by setting the property grails.sitemesh.default.layout in grails-app/conf/Config.groovy. The value of the property maps to the filename of the layout. For example if we set the value to main then Grails will use grails-app/views/layouts/main.gsp.

Another way is to create a layout with the name application.gsp and save it in grails-app/views/layouts. Grails will use this layout if the layout cannot be determined in another way.

February 22, 2011

Grails Goodness: Encode Content with the encodeAs Tag

Encoding and decoding values is easy in Grails. We can invoke the dynamic methods encodeAs...() and decodeAs...() for several codecs. If we have a large block of content on our Groovy Server Page (GSP) we want to encode we can use the <g:encodeAs codec="..."/> tag. The body of the tag is encoded with the codec we specify with the codec attribute.

<%-- Sample.gsp --%>

<h1>Sample Title</h1>

<g:encodeAs codec="HTML">
<h1>Sample Title</h1>
</g:encodeAs>    

If we look at the generated HTML source we see:

<h1>Sample Title</h1>

&lt;h1&gt;Sample Title&lt;/h1&gt;

February 21, 2011

Grails Goodness: Format Boolean Values with the formatBoolean Tag

If we want to display something else than true or false for a boolean value on our Groovy Server Page (GSP) we can use the formatBoolean tag. We can specify a value for true with the true attribute and for false with the false attribute. If we don't specify a true or false attribute Grails will look for the key boolean.true and boolean.false in the i18n resource bundle. If those are not found than the keys default.boolean.true and default.boolean.false are used. And the last fallback are the values True for true and False for false boolean values.

// File: grails-app/i18n/messages.properties
boolean.true=Okay
boolean.false=Not okay
<%-- Sample.gsp --%>

<g:formatBoolean boolean="${true}"/> outputs: Okay
<g:formatBoolean boolean="${false}"/> outputs: Not okay

<g:formatBoolean boolean="${true}" true="Yes" false="No"/> outputs: Yes
<g:formatBoolean boolean="${false}" true="Yes" false="No"/> outputs: No