(Quick Reference)

1.1.3 Web层特性 - Reference Documentation

Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith

Version: null

1.1.3 Web层特性

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 = {

}

使用函数方法来定义控制器的动作

以前Grails的动作只能通过闭包来定义,现在通过一般的函数方法也可以定义了,实际上这也是优先推荐的方式,比如:

// 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
}

自动绑定带参数的动作方法

现在可以将表单(form)参数跟动作参数进行自动匹配了,只要表单下边子元素的名称名字跟参数名称一致即可,以如下的表单为例:

<g:form name="myForm" action="save">
    <input name="name" />
    <input name="age" />
</g:form>

你可以定义其动作参数为每一个输入元素的名字,系统会自动将参数值转换为其响应的类型,如以下代码所示:

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.

静态资源

新引入的静态资源抽象,可以声明式地处理JavaScript、CSS以及图像资源的自动排序、压缩、缓存.

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()
    }
}

Servlet 3.0 的异步特性

Grails现在已经支持Servlet 3.0了,且包含了其规范中定义的异步编程模型,比如:

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 grailsLinkGenerator

def generateLink() { grailsLinkGenerator.link(controller:"book", action:"list") }

生成超链接的API

在Grails应用,新增的通用LinkGenerator类,可以在任何地方生成超链接了,不像以前,只能局限于控制器的上下文中。比如,你要在一个服务或者异步的后台任务等超出web请求范围内使用,可以参考如下代码:

LinkGenerator grailsLinkGenerator

def 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 groovyPageRenderer

void 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)
}

页面渲染API

LinkGenerator类似,新增的PageRenderer能够在超出web请求范围之外的任何地方渲染GSP页面,比如被调度的任务或者WEB服务接口中。PageRenderer 类的API跟在控制器中使用的render方法很类似,比如:

grails.gsp.PageRenderer groovyPageRenderer

void welcomeUser(User user) { def contents = groovyPageRenderer.render(view:"/emails/welcomeLetter", model:[user: user]) sendEmail { to user.email body contents } }

PageRenderer服务还允许你将GSP页面预处理成为HTML模板:

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 = { // … } }

过滤器的排除

过滤器现在可以明确的指定是排除控制器、动作、还是URI,这为特定请求的过滤器提供了更多选项。

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.

性能的提升

通过将GSP编译器优化成内联方法,使得GSP的页面渲染性能又一次得到提升。

HTML5 Scaffolding

There is a new HTML5-based scaffolding UI:

HTML5脚手架

新增的基于HTML5的脚手架界面如下:

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.

jQuery作为缺省JavaScript库

jQuery插件已经作为一个缺省的JavaScript库被安装到Grails应用当中。因为向后兼容的原因,Prototype插件依然是有效的,其安装指令请参考Prototype插件官方文档

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')

易用的日期解析

params对象新增了一个date方法,用以轻松地、空指针安全地解析日期,比如:

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')