Dynamic Encoding Methods

Dynamic Encoding Methods

Overview

As of version 0.4 Grails adds support for dynamic encode/decode methods.  A set of standard codecs are bundled with Grails.  Grails also supports a simple mechanism for developers to contribute their own codecs that will be recognized at runtime.

Codec Classes

A Grails codec class is a class that may contain an encode closure, a decode closure or both.  When a Grails application starts up the Grails framework will dynamically load codecs from the grails-app/utils/ directory. 

The framework will look under grails-app/utils/ for class names that end with the word 'Codec'.  For example one of the standard codecs that ship with Grails is grails-app/utils/HTMLCodec.groovy

If a codec contains an encode closure Grails will create a dynamic encode method and add that method to the String class with a name representing the codec that defined the encode closure.  For example, the HTMLCodec class defines an encode closure so Grails will attach that closure to the String class with the name encodeAsHTML.

The HTMLCodec and URLCodec classes also define a decode closure so Grails will attach those closures to the String class with the names decodeHTML and decodeURL. Dynamic codec methods may be invoked from anywhere in a Grails application. For example, consider a case where a report contains a property called 'description' and that description may contain special characters that need to be escaped to be presented in an HTML document.  One way to deal with that in a GSP is to encode the description property using the dynamic encode method as shown below:

${report.description.encodeAsHTML()}

Decoding is performed using value.decodeHTML() syntax.

Standard Codecs

HTMLCodec

This codec perfoms HTML escaping and unescaping, so that values you provide can be rendered safely in an HTML page without creating any HTML tags or damaging the page layout. For example, given a value "Don't you know that 2 > 1?" you wouldn't be able to show this safely within an HTML page because the > will look like it closes a tag, which is especially bad if you render this data within an attribute, such as the value attribute of an input field.

Example of usage:

<input name="comment.message" value="${comment.message.encodeAsHTML()}"/>

Note that the HTML encoding does not re-encode apostrophe/single quote so you must use double quotes on attribute values to avoid text with apostrophes messing up your page.

URLCodec

URL encoding is required when creating URLs in links or form actions, or any time data may be used to create a URL. It prevents illegal characters getting into the URL to change its meaning, for example a "Apple & Blackberry" is not going to work well as a parameter in a GET request as the ampersand will break the parsing of parameters.

Example of usage:

<a href="/mycontroller/find?searchKey=${lastSearch.encodeAsURL()}">Repeat last search</a>

Base64Codec

Performs Base64 encode/decode functions.
Example of usage:

Your registration code is: user.registrationCode.encodeAsBase64()

JavaScriptCodec

Example of usage:

Element.update('${elementId}', '${render(template: "/common/message").encodeAsJavaScript()}')

Custom Codecs

Applications may define their own codecs and Grails will load them along with the standard codecs. A custom codec class must be defined in the grails-app/utils/ directory and the class name must end with 'Codec'. The codec may contain a static encode closure, a static decode closure or both. The closure should expect a single argument which will be the object that the dynamic method was invoked on.

grails-app/utils/PigLatinCodec.groovy
class PigLatinCodec {

  static encode = { str ->

    // convert the string to piglatin and return the result

  }

}

With that codec in place an application could do something like this:

${lastName.encodeAsPigLatin()}

Labels

 
(None)