1.5.3 Web Features 2.4.4

See the light - agile, industrial strength, rapid web application development made easy

(Quick Reference) # 1.5.3 Web Features - Reference Documentation Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari Version: 2.4.4 ## 1.5.3 Web Features #### Controller Actions as Methods It is now possible to define controller actions as methods instead of using closures as in previous versions of Grails. In fact this is now the preferred way of expressing an action. For example:

    // action as a method
    def index() {}
    // action as a closure
    def index = {}
#### Binding Primitive Method Action Arguments It is now possible to bind form parameters to action arguments where the name of the form element matches the argument name. For example given the following form:
    <g:form name="myForm" action="save">
        <input name="name" />
        <input name="age" />
    </g:form>
You can define an action that declares arguments for each input and automatically converts the parameters to the appropriate type:
    def save(String name, int age) {
        // remaining
    }
#### Static Resource Abstraction A new static resource abstraction is included that allows declarative handling of JavaScript, CSS and image resources including automatic ordering, compression, caching and gzip handling. #### Servlet 3.0 Async Features Grails now supports Servlet 3.0 including the Asynchronous programming model defined by the specification:
    def index() {
        def ctx = startAsync()
        ctx.start {
            new Book(title:"The Stand").save()
            render template:"books", model:[books:Book.list()]
            ctx.complete()
        }
    }
#### Link Generation API A general purpose `LinkGenerator` class is now available that is usable anywhere within a Grails application and not just within the context of a controller. For example if you need to generate links in a service or an asynchronous background job outside the scope of a request:

    LinkGenerator grailsLinkGeneratordef generateLink() {
        grailsLinkGenerator.link(controller:"book", action:"list")
    }
#### Page Rendering API Like the `LinkGenerator` the new `PageRenderer` can be used to render GSP pages outside the scope of a web request, such as in a scheduled job or web service. The `PageRenderer` class features a very similar API to the `render` method found within controllers:

    grails.gsp.PageRenderer groovyPageRenderervoid welcomeUser(User user) {
        def contents = groovyPageRenderer.render(view:"/emails/welcomeLetter", model:[user: user])
        sendEmail {
            to user.email
            body contents
        }
    }
The `PageRenderer` service also allows you to pre-process GSPs into HTML templates:
    new File("/path/to/welcome.html").withWriter { w ->
        groovyPageRenderer.renderTo(view:"/page/content", w)
    }
#### Filter Exclusions Filters may now express controller, action and uri exclusions to offer more options for expressing to which requests a particular filter should be applied.

    filter1(actionExclude: 'log*') {
        before = {
            // …
        }
    }
    filter2(controllerExclude: 'auth') {
        before = {
            // …
        }
    }filter3(uriExclude: '/secure*') {
        before = {
            // …
        }
    }
#### Performance Improvements Performance of GSP page rendering has once again been improved by optimizing the GSP compiler to inline method calls where possible. #### HTML5 Scaffolding There is a new HTML5-based scaffolding UI: #### jQuery by Default The jQuery plugin is now the default JavaScript library installed into a Grails application. For backwards compatibility a Prototype plugin is available. Refer to the documentation on the Prototype plugin for installation instructions. #### Easy Date Parsing A new `date` method has been added to the `params` object to allow easy, null-safe parsing of dates:

    def val = params.date('myDate', 'dd-MM-yyyy')// or a list for formats
    def val = params.date('myDate', ['yyyy-MM-dd', 'yyyyMMdd', 'yyMMdd'])// or the format read from messages.properties via the key 'date.myDate.format'
    def val = params.date('myDate')
#### Customizable URL Formats The default URL Mapping mechanism supports camel case names in the URLs. The default URL for accessing an action named `addNumbers` in a controller named `MathHelperController` would be something like `/mathHelper/addNumbers`. Grails allows for the customization of this pattern and provides an implementation which replaces the camel case convention with a hyphenated convention that would support URLs like `/math-helper/add-numbers`. To enable hyphenated URLs assign a value of "hyphenated" to the `grails.web.url.converter` property in `grails-app/conf/Config.groovy`.

    // grails-app/conf/Config.groovygrails.web.url.converter = 'hyphenated'
Arbitrary strategies may be plugged in by providing a class which implements the UrlConverter interface and adding an instance of that class to the Spring application context with the bean name of `grails.web.UrlConverter.BEAN_NAME`. If Grails finds a bean in the context with that name, it will be used as the default converter and there is no need to assign a value to the `grails.web.url.converter` config property.

    // src/groovy/com/myapplication/MyUrlConverterImpl.groovypackage com.myapplicationclass MyUrlConverterImpl implements grails.web.UrlConverter {    String toUrlElement(String propertyOrClassName) {
            // return some representation of a property or class name that should be used in URLs…
        }
    }

    // grails-app/conf/spring/resources.groovybeans = {
        "${grails.web.UrlConverter.BEAN_NAME}"(com.myapplication.MyUrlConverterImpl)
    }
#### Web Flow input and output It is now possible to provide input arguments when calling a subflow. Flows can also return output values that can be used in a calling flow. | Quick Reference (hide)