(Quick Reference)

6.1 控制器 - Reference Documentation

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

Version: null

6.1 控制器

A controller handles requests and creates or prepares the response. A controller can generate the response directly or delegate to a view. To create a controller, simply create a class whose name ends with Controller in the grails-app/controllers directory (in a subdirectory if it's in a package).

The default URL Mapping configuration ensures that the first part of your controller name is mapped to a URI and each action defined within your controller maps to URIs within the controller name URI.

一个控制器通常用以处理请求,创建或者准备响应,也能直接生成响应或者委托给一个视图。要创建一个控制器,只需要在grails-app/controllers目录(如果有包的话,要位于相应的子目录下)下简单创建一个名字以Controller结尾的类。

默认的URL映射配置能确保你的控制器名字的第一部分被映射到一个URI上,而控制器中的每个操作定义被映射到控制器命名URI中的URI中。

6.1.1 理解控制器和操作

Creating a controller

Controllers can be created with the create-controller or generate-controller command. For example try running the following command from the root of a Grails project:

grails create-controller book

The command will create a controller at the location grails-app/controllers/myapp/BookController.groovy:

package myapp

class BookController {

def index() { } }

where "myapp" will be the name of your application, the default package name if one isn't specified.

BookController by default maps to the /book URI (relative to your application root).

The create-controller and generate-controller commands are just for convenience and you can just as easily create controllers using your favorite text editor or IDE

创建控制器

控制器可以通过create-controller或者generate-controller命令创建。比如,在Grails工程的根目录中运行如下命令:

grails create-controller book

此命令将创建一个位于grails-app/controllers/myapp/BookController.groovy的控制器:

package myapp

class BookController {

def index() { } }

此处"myapp"是你应用的名称,如果你没有指定包名的话,缺省的包名就是应用名称。

BookController缺省被映射于URI /book (相对于你应用上下文的根而言)

create-controllergenerate-controller命令只是便利方法而已,你也可以使用你喜欢的文本编辑器或者IDE来轻松的创建控制器。

Creating Actions

A controller can have multiple public action methods; each one maps to a URI:

class BookController {

def list() {

// do controller logic // create model

return model } }

This example maps to the /book/list URI by default thanks to the property being named list.

创建操作

一个控制器可以有多个公共操作方法,每一个都映射于一个URI:

class BookController {

def list() {

// do controller logic // create model

return model } }

缺省情况下,这个示例将映射到URI /book/list,这要归功于list属性。

Public Methods as Actions

In earlier versions of Grails actions were implemented with Closures. This is still supported, but the preferred approach is to use methods.

Leveraging methods instead of Closure properties has some advantages:

  • Memory efficient
  • Allow use of stateless controllers (singleton scope)
  • You can override actions from subclasses and call the overridden superclass method with super.actionName()
  • Methods can be intercepted with standard proxying mechanisms, something that is complicated to do with Closures since they're fields.

If you prefer the Closure syntax or have older controller classes created in earlier versions of Grails and still want the advantages of using methods, you can set the grails.compile.artefacts.closures.convert property to true in BuildConfig.groovy:

grails.compile.artefacts.closures.convert = true

and a compile-time AST transformation will convert your Closures to methods in the generated bytecode.

If a controller class extends some other class which is not defined under the grails-app/controllers/ directory, methods inherited from that class are not converted to controller actions. If the intent is to expose those inherited methods as controller actions the methods may be overridden in the subclass and the subclass method may invoke the method in the super class.

公共方法作为操作

在以前版本的Grails中,操作是通过闭包来实现的。现在依然是支持的,不过更推荐使用方法的方式来实现。

使用方法来替代闭包有如下一些优点:

  • 更高效的内存
  • 允许使用状态无关的控制器(作用域是singleton
  • 你可以在子类中重载操作,并且可以使用super.actionName()调用父类的方法
  • 方法可以通过标准的代理机制进行拦截,同样的事情闭包更复杂一些,因为它们是属性字段。

如果你更喜欢闭包的语法或者现有的控制器类是以前版本的Grails所创建的,又想得到方法作为操作的好处,你可以设置BuildConfig.groovy中的grails.compile.artefacts.closures.convert属性为true:

grails.compile.artefacts.closures.convert = true

这时,一个编译时的AST变换会在生成字节码的时候,将你的闭包转变为方法

如果一个控制器类继承于其他类,但不是被定义在grails-app/controllers/目录下,那么继承过来的方法将不会被转换成控制器操作的。如果目标是为了能将那些继承来的方法暴露为控制器操作,那么这些方法应该是能够被子类重载的,并且在子类中也可以调用其父类的方法。

The Default Action

A controller has the concept of a default URI that maps to the root URI of the controller, for example /book for BookController. The action that is called when the default URI is requested is dictated by the following rules:

  • If there is only one action, it's the default
  • If you have an action named index, it's the default
  • Alternatively you can set it explicitly with the defaultAction property:

static defaultAction = "list"

缺省操作

一个控制器即映射到控制器的根URI。默认情况下缺省URI在这里的是/book。默认的URI通过以下规则来支配: 一个控制器具有默认URI的概念,其将映射到控制器的根URI。比如BookController映射到/book。当缺省URI被请求时,会根据以下规则来调用操作:

  • 如果仅有一个操作,那么它就是那个缺省操作
  • 如果你有一个index操作,那么它就是缺省的
  • 或者你可以使用defaultAction属性来明确指定:

static defaultAction = "list"

6.1.2 控制器和作用域

Available Scopes

Scopes are hash-like objects where you can store variables. The following scopes are available to controllers:

  • servletContext - Also known as application scope, this scope lets you share state across the entire web application. The servletContext is an instance of ServletContext
  • session - The session allows associating state with a given user and typically uses cookies to associate a session with a client. The session object is an instance of HttpSession
  • request - The request object allows the storage of objects for the current request only. The request object is an instance of HttpServletRequest
  • params - Mutable map of incoming request query string or POST parameters
  • flash - See below

有效作用域

作用域就像是hash对象,允许你存储变量。以下是控制器有效作用域:

  • servletContext - 也被叫做应用级别范围,它允许你共享整个web应用的状态。 servletContext对象是ServletContext的一个实例
  • session - 会话(session)允许关联某个用户的状态,通常使用Cookie把会话与客户端关联起来。session对象是HttpSession的一个实例
  • request - 请求对象仅为当前的请求存储对象。request对象是HttpServletRequest的一个实例
  • params - 带查询字串(query string)或者POST参数输入请求的可变map
  • flash - 见下文

Accessing Scopes

Scopes can be accessed using the variable names above in combination with Groovy's array index operator, even on classes provided by the Servlet API such as the HttpServletRequest:

class BookController {
    def find() {
        def findBy = params["findBy"]
        def appContext = request["foo"]
        def loggedUser = session["logged_user"]
    }
}

You can also access values within scopes using the de-reference operator, making the syntax even more clear:

class BookController {
    def find() {
        def findBy = params.findBy
        def appContext = request.foo
        def loggedUser = session.logged_user
    }
}

This is one of the ways that Grails unifies access to the different scopes.

访问作用域

作用域可以通过上述提到的变量名和Groovy的数组索引操作符的方式来访问,即使这些类是Servlet API的类,例如HttpServletRequest也可以用,比如:

class BookController {
    def find() {
        def findBy = params["findBy"]
        def appContext = request["foo"]
        def loggedUser = session["logged_user"]
    }
}

你甚至可以使用"."操作符来访问作用域的值,这样使语法更加简洁清晰:

class BookController {
    def find() {
        def findBy = params.findBy
        def appContext = request.foo
        def loggedUser = session.logged_user
    }
}

这是统一访问不同作用域的方式之一。

Using Flash Scope

Grails supports the concept of flash scope as a temporary store to make attributes available for this request and the next request only. Afterwards the attributes are cleared. This is useful for setting a message directly before redirecting, for example:

def delete() {
    def b = Book.get(params.id)
    if (!b) {
        flash.message = "User not found for id ${params.id}"
        redirect(action:list)
    }
    … // remaining code
}

When the list action is requested, the message value will be in scope and can be used to display an information message. It will be removed from the flash scope after this second request.

Note that the attribute name can be anything you want, and the values are often strings used to display messages, but can be any object type.

使用Flash作用域

Grails支持flash作用域的概念,它只存贮本次请求和下次请求之间临时用到的属性,随后属性值将被清除。这在重定向之前,设置提示消息是非常有用的,比如:

def delete() {
    def b = Book.get(params.id)
    if (!b) {
        flash.message = "User not found for id ${params.id}"
        redirect(action:list)
    }
    … // remaining code
}

list操作被请求时,message的值在此范围内有效,可以用以显示一个提示信息。在第二次请求的时候,此值将从flash作用域移除。

注意,属性的名称可以是你期望的任何东西,其值多是用以显示信息的字符串,不过也可以是任何对象。

Scoped Controllers

By default, a new controller instance is created for each request. In fact, because the controller is prototype scoped, it is thread-safe since each request happens on its own thread.

You can change this behaviour by placing a controller in a particular scope. The supported scopes are:

  • prototype (default) - A new controller will be created for each request (recommended for actions as Closure properties)
  • session - One controller is created for the scope of a user session
  • singleton - Only one instance of the controller ever exists (recommended for actions as methods)

To enable one of the scopes, add a static scope property to your class with one of the valid scope values listed above, for example

static scope = "singleton"

You can define the default strategy under in Config.groovy with the grails.controllers.defaultScope key, for example:

grails.controllers.defaultScope = "singleton"

Use scoped controllers wisely. For instance, we don't recommend having any properties in a singleton-scoped controller since they will be shared for all requests. Setting a default scope other than prototype may also lead to unexpected behaviors if you have controllers provided by installed plugins that expect that the scope is prototype.

控制器的作用域

通常,每一个请求会创建一个控制器实例。事实上,是因为控制器的作用域是prototype,并且每个请求都有自己的线程,所以控制器是线程安全的。

不过,你还是可以在控制器内放置一个特定的作用域来改变这种行为。其支持的作用域如下:

  • prototype (缺省) - 每一次请求创建一个新的控制器实例(当操作为必包属性时推荐使用)
  • session - 在一个用户会话的作用域内只创建一个控制器实例
  • singleton - 自始自终只有一个控制器实例(当操作时一个方法时推荐使用)

要想使用上述的作用域,请在类内增加一个静态的scope属性,并且使用上述作用域之一为其赋值,比如:

static scope = "singleton"

你也可以在Config.groovy中改变grails.controllers.defaultScope的值来改变缺省策略,比如

grails.controllers.defaultScope = "singleton"

请明智的使用控制器的作用域。比如我们不推荐singleton作用域的控制器有任何属性,因为他们将在 所有 的请求共享。此外,如果你安装的插件的控制器是prototype的,那么修改缺省的prototype作用域也可能导致不可预知的行为。

6.1.3 模型和视图

Returning the Model

A model is a Map that the view uses when rendering. The keys within that Map correspond to variable names accessible by the view. There are a couple of ways to return a model. First, you can explicitly return a Map instance:

def show() {
    [book: Book.get(params.id)]
}

The above does not reflect what you should use with the scaffolding views - see the scaffolding section for more details.

If no explicit model is returned the controller's properties will be used as the model, thus allowing you to write code like this:

class BookController {

List books List authors

def list() { books = Book.list() authors = Author.list() } }

This is possible due to the fact that controllers are prototype scoped. In other words a new controller is created for each request. Otherwise code such as the above would not be thread-safe, and all users would share the same data.

In the above example the books and authors properties will be available in the view.

A more advanced approach is to return an instance of the Spring ModelAndView class:

import org.springframework.web.servlet.ModelAndView

def index() { // get some books just for the index page, perhaps your favorites def favoriteBooks = ...

// forward to the list view to show them return new ModelAndView("/book/list", [ bookList : favoriteBooks ]) }

One thing to bear in mind is that certain variable names can not be used in your model:

  • attributes
  • application

Currently, no error will be reported if you do use them, but this will hopefully change in a future version of Grails.

返回模型

模型是在渲染的时候给视图用的一个映射(Map)。映射的键对应于视图中的命名变量。有很多方法都可以返回一个模型。首先,你可以通过明确返回映射(Map)实例的方式:

def show() {
    [book: Book.get(params.id)]
}

上述示例并 不会 影响到脚手架的视图-更多信息请参考脚手架章节

如果没有明确指定模型,那么控制器的属性将作为模型返回给视图,象如下代码所示那样:

class BookController {

List books List authors

def list() { books = Book.list() authors = Author.list() } }

这是可行的,因为控制器的缺省作用域是prototype。换句话说,每一个请求都将创建一个新的控制器。否则的话,上述的代码就不是线程安全的了,所有的用户将共享同样的数据。

在上述示例中,booksauthors属性在视图中将是有效的。

另外一个更高级的方式是返回一个Spring ModelAndView类的一个实例:

import org.springframework.web.servlet.ModelAndView

def index() { // get some books just for the index page, perhaps your favorites def favoriteBooks = ...

// forward to the list view to show them return new ModelAndView("/book/list", [ bookList : favoriteBooks ]) }

只有一件事情需要担心,那就是在你的模型中可能某些变量名称是不可用的:

  • attributes
  • application

当前,如果你使用到了它们,也不会有任何错误提示报告出来,但是在将来的Grails版本中希望有所改善。

Selecting the View

In both of the previous two examples there was no code that specified which view to render. So how does Grails know which one to pick? The answer lies in the conventions. Grails will look for a view at the location grails-app/views/book/show.gsp for this list action:

class BookController {
    def show() {
         [book: Book.get(params.id)]
    }
}

To render a different view, use the render method:

def show() {
    def map = [book: Book.get(params.id)]
    render(view: "display", model: map)
}

In this case Grails will attempt to render a view at the location grails-app/views/book/display.gsp. Notice that Grails automatically qualifies the view location with the book directory of the grails-app/views directory. This is convenient, but to access shared views you need instead you can use an absolute path instead of a relative one:

def show() {
    def map = [book: Book.get(params.id)]
    render(view: "/shared/display", model: map)
}

In this case Grails will attempt to render a view at the location grails-app/views/shared/display.gsp.

Grails also supports JSPs as views, so if a GSP isn't found in the expected location but a JSP is, it will be used instead.

选择视图

在前面的两个示例中,我们并没有指定要用那个视图来渲染。那么Grails是如何知道那个将被选择?答案是规约依赖。在下面的示例中,Grails将自动的寻找位于grails-app/views/book/show.gsp的视图:

class BookController {
    def show() {
         [book: Book.get(params.id)]
    }
}

要渲染另外不同的视图,需要使用render方法:

def show() {
    def map = [book: Book.get(params.id)]
    render(view: "display", model: map)
}

在上述示例中,Grails将尝试使用视图grails-app/views/book/display.gsp来渲染。注意,Grails会根据grails-app/views下的book目录来自动地限定视图的位置。常规是这样的,但是要访问那些共享的视图,你还是需要使用绝对路径来替代相对路径:

def show() {
    def map = [book: Book.get(params.id)]
    render(view: "/shared/display", model: map)
}

在这个示例中,Grails将尝试使用视图grails-app/views/shared/display.gsp来渲染。

Grails也支持JSP的视图,因此如果一个预期的GSP没有找到,但是有相应的JSP,那么它将使用此JSP。

Rendering a Response

Sometimes it's easier (for example with Ajax applications) to render snippets of text or code to the response directly from the controller. For this, the highly flexible render method can be used:

render "Hello World!"

The above code writes the text "Hello World!" to the response. Other examples include:

// write some markup
render {
   for (b in books) {
      div(id: b.id, b.title)
   }
}

// render a specific view
render(view: 'show')

// render a template for each item in a collection
render(template: 'book_template', collection: Book.list())

// render some text with encoding and content type
render(text: "<xml>some xml</xml>", contentType: "text/xml", encoding: "UTF-8")

If you plan on using Groovy's MarkupBuilder to generate HTML for use with the render method be careful of naming clashes between HTML elements and Grails tags, for example:

import groovy.xml.MarkupBuilder
…
def login() {
    def writer = new StringWriter()
    def builder = new MarkupBuilder(writer)
    builder.html {
        head {
            title 'Log in'
        }
        body {
            h1 'Hello'
            form {
            }
        }
    }

def html = writer.toString() render html }

This will actually call the form tag (which will return some text that will be ignored by the MarkupBuilder). To correctly output a <form> element, use the following:

def login() {
    // …
    body {
        h1 'Hello'
        builder.form {
        }
    }
    // …
}

渲染响应

有时候在控制器中直接渲染文本或者代码片段到响应是很容易的(比如Ajax的应用)。这时,就可以使用灵活性很高的render方法了:

render "Hello World!"

上述代码输出"Hello World!"文本到其响应。另外的例子还包括:

// write some markup
render {
   for (b in books) {
      div(id: b.id, b.title)
   }
}

// render a specific view
render(view: 'show')

// render a template for each item in a collection
render(template: 'book_template', collection: Book.list())

// render some text with encoding and content type
render(text: "<xml>some xml</xml>", contentType: "text/xml", encoding: "UTF-8")

如果你打算使用Groovy的MarkupBuilder来生成HTML来供render方法使用,这时,你要注意HTML元素和Grails标签名字的冲突,比如:

import groovy.xml.MarkupBuilder
…
def login() {
    def writer = new StringWriter()
    def builder = new MarkupBuilder(writer)
    builder.html {
        head {
            title 'Log in'
        }
        body {
            h1 'Hello'
            form {
            }
        }
    }

def html = writer.toString() render html }

这将实际调用form标签(其返回的文本将被MarkupBuilder忽略)。要正确地输出<form>元素,需要使用如下的例子:

def login() {
    // …
    body {
        h1 'Hello'
        builder.form {
        }
    }
    // …
}

6.1.4 重定向和链(Chaining)

Redirects

Actions can be redirected using the redirect controller method:

class OverviewController {

def login() {}

def find() { if (!session.user) redirect(action: 'login') return } … } }

Internally the redirect method uses the HttpServletResponse object's sendRedirect method.

The redirect method expects one of:

  • Another closure within the same controller class:

// Call the login action within the same class
redirect(action: login)
  • The name of an action (and controller name if the redirect isn't to an action in the current controller):

// Also redirects to the index action in the home controller
redirect(controller: 'home', action: 'index')
  • A URI for a resource relative the application context path:

// Redirect to an explicit URI
redirect(uri: "/login.html")
  • Or a full URL:

// Redirect to a URL
redirect(url: "http://grails.org")

Parameters can optionally be passed from one action to the next using the params argument of the method:

redirect(action: 'myaction', params: [myparam: "myvalue"])

These parameters are made available through the params dynamic property that accesses request parameters. If a parameter is specified with the same name as a request parameter, the request parameter is overridden and the controller parameter is used.

Since the params object is a Map, you can use it to pass the current request parameters from one action to the next:

redirect(action: "next", params: params)

Finally, you can also include a fragment in the target URI:

redirect(controller: "test", action: "show", fragment: "profile")

which will (depending on the URL mappings) redirect to something like "/myapp/test/show#profile".

重定向

操作可以使用控制器的redirect方法进行重定向:

class OverviewController {

def login() {}

def find() { if (!session.user) redirect(action: 'login') return } … } }

本质上redirect方法是使用HttpServletResponse对象的sendRedirect方法来工作的。

redirect方法的重定向目标用法如下:

  • 同一控制器类的另外一个闭包:

// Call the login action within the same class
redirect(action: login)
  • 操作的名称(如果要重定向的操作名不在同一个控制器内,还需要制定控制器名):

// Also redirects to the index action in the home controller
redirect(controller: 'home', action: 'index')
  • 一个相对于应用上下文路径的URI

// Redirect to an explicit URI
redirect(uri: "/login.html")
  • 或者一个完整的URL:

// Redirect to a URL
redirect(url: "http://grails.org")

从一个操作到下一个之间的参数是可选的,这可以使用此方法的params来实现:

redirect(action: 'myaction', params: [myparam: "myvalue"])

这些参数是有效的,因为它们是通过可以访问请求参数的params来实现的。如果一个参数跟请求参数同名,那么请求参数将被覆盖,控制器的参数将被优先使用。

由于这个params对象就是一个Map,因此你可以使用它将当前的请求参数从一个操作传递到下个操作:

redirect(action: "next", params: params)

最后,你还可以在目标URI中包含片段(fragment):

redirect(controller: "test", action: "show", fragment: "profile")

将重定向到类似于 "/myapp/test/show#profile" 的目标(依赖于URL映射)。

Chaining

Actions can also be chained. Chaining allows the model to be retained from one action to the next. For example calling the first action in this action:

class ExampleChainController {

def first() { chain(action: second, model: [one: 1]) }

def second () { chain(action: third, model: [two: 2]) }

def third() { [three: 3]) } }

results in the model:

[one: 1, two: 2, three: 3]

The model can be accessed in subsequent controller actions in the chain using the chainModel map. This dynamic property only exists in actions following the call to the chain method:

class ChainController {

def nextInChain() { def model = chainModel.myModel … } }

Like the redirect method you can also pass parameters to the chain method:

chain(action: "action1", model: [one: 1], params: [myparam: "param1"])

链(Chaining)

操作同样可以作为一个链。在从一个操作传递到下个操作的时候,链一直保留着其模型。比如下面调用first操作的示例:

class ExampleChainController {

def first() { chain(action: second, model: [one: 1]) }

def second () { chain(action: third, model: [two: 2]) }

def third() { [three: 3]) } }

此模型的结果是:

[one: 1, two: 2, three: 3]

此模型可以在随后控制器的操作中通过chainModel映射来访问。此动态属性只存在于调用chain方法的操作中:

class ChainController {

def nextInChain() { def model = chainModel.myModel … } }

redirect方法一样,你可以传递参数给chain方法:

chain(action: "action1", model: [one: 1], params: [myparam: "param1"])

6.1.5 控制器拦截器

Often it is useful to intercept processing based on either request, session or application state. This can be achieved with action interceptors. There are currently two types of interceptors: before and after.

If your interceptor is likely to apply to more than one controller, you are almost certainly better off writing a Filter. Filters can be applied to multiple controllers or URIs without the need to change the logic of each controller

通常情况下,拦截处理请求、会话或者应用的状态数据是非常有用的。这个可以通过操作的拦截器来完成。当前有两种类型的拦截器:before和after。

如果你的拦截可能用于一个以上控制器的话,你最好写一个过滤器。过滤器可以在不需要改变每个控制器逻辑的情况下,应用于多个控制器或者URI。

Before Interception

The beforeInterceptor intercepts processing before the action is executed. If it returns false then the intercepted action will not be executed. The interceptor can be defined for all actions in a controller as follows:

def beforeInterceptor = {
    println "Tracing action ${actionUri}"
}

The above is declared inside the body of the controller definition. It will be executed before all actions and does not interfere with processing. A common use case is very simplistic authentication:

def beforeInterceptor = [action: this.&auth, except: 'login']

// defined with private scope, so it's not considered an action private auth() { if (!session.user) { redirect(action: 'login') return false } }

def login() { // display login page }

The above code defines a method called auth. A private method is used so that it is not exposed as an action to the outside world. The beforeInterceptor then defines an interceptor that is used on all actions except the login action and it executes the auth method. The auth method is referenced using Groovy's method pointer syntax. Within the method it detects whether there is a user in the session, and if not it redirects to the login action and returns false, causing the intercepted action to not be processed.

前拦截

beforeInterceptor在操作被执行以前进行拦截处理。如果其返回值是false,那么被拦截的操作将得不到执行。拦截对控制器的所有操作进行定义,如下所示:

def beforeInterceptor = {
    println "Tracing action ${actionUri}"
}

上述代码被声明于控制器定义的主体内。它将在每一个操作执行之前被调用,在此处并不做任何干涉。一个通用的用例就是简单地身份验证:

def beforeInterceptor = [action: this.&auth, except: 'login']

// defined with private scope, so it's not considered an action private auth() { if (!session.user) { redirect(action: 'login') return false } }

def login() { // display login page }

上述代码定义了一个auth方法。这个私有方法通常用于避免被暴露为一个操作,从而也就不会被从外面访问到。接着beforeInterceptor定义了一个应用于所有操作的拦截器,login操作除外,此拦截器将先执行auth方法。auth方法的引用是Groovy方法的指针语法。此方法检查一个用户是否存在于会话中,如果不存在,那么将重定向到login操作并且返回false,这样那些被拦截过的操作也就不会被处理。

After Interception

Use the afterInterceptor property to define an interceptor that is executed after an action:

def afterInterceptor = { model ->
    println "Tracing action ${actionUri}"
}

The after interceptor takes the resulting model as an argument and can hence manipulate the model or response.

An after interceptor may also modify the Spring MVC ModelAndView object prior to rendering. In this case, the above example becomes:

def afterInterceptor = { model, modelAndView ->
    println "Current view is ${modelAndView.viewName}"
    if (model.someVar) modelAndView.viewName = "/mycontroller/someotherview"
    println "View is now ${modelAndView.viewName}"
}

This allows the view to be changed based on the model returned by the current action. Note that the modelAndView may be null if the action being intercepted called redirect or render.

后拦截

使用afterInterceptor属性可以定义一个操作执行之后的拦截器:

def afterInterceptor = { model ->
    println "Tracing action ${actionUri}"
}

后拦截器使用一个返回结果的model作为参数,因此也就可以操作模型和响应。

一个后拦截器可以在渲染之前修改Spring MVC的ModelAndView对象。此种情况下,上述的示例将变为:

def afterInterceptor = { model, modelAndView ->
    println "Current view is ${modelAndView.viewName}"
    if (model.someVar) modelAndView.viewName = "/mycontroller/someotherview"
    println "View is now ${modelAndView.viewName}"
}

上述示例中,允许当前操作返回之前,可以根据模型来修改视图。不过要注意的是:如果被拦截的操作调用了redirect或者render,其modelAndView可能是null

Interception Conditions

Rails users will be familiar with the authentication example and how the 'except' condition was used when executing the interceptor (interceptors are called 'filters' in Rails; this terminology conflicts with Servlet filter terminology in Java):

def beforeInterceptor = [action: this.&auth, except: 'login']

This executes the interceptor for all actions except the specified action. A list of actions can also be defined as follows:

def beforeInterceptor = [action: this.&auth, except: ['login', 'register']]

The other supported condition is 'only', this executes the interceptor for only the specified action(s):

def beforeInterceptor = [action: this.&auth, only: ['secure']]

拦截条件

Rails用户将很熟悉验证的示例以及在执行拦截的时候如何使用'except'条件(在Rails中,拦截器被称为‘过滤器’;这个术语跟Java中Servlet的过滤器冲突):

def beforeInterceptor = [action: this.&auth, except: 'login']

除了给定的操作之外,这将在所有操作之前执行拦截。操作的列表也可以被定义为如下所示:

def beforeInterceptor = [action: this.&auth, except: ['login', 'register']]

另外所支持的条件是‘only’,它将仅为特定的操作执行拦截:

def beforeInterceptor = [action: this.&auth, only: ['secure']]

6.1.6 数据绑定

Data binding is the act of "binding" incoming request parameters onto the properties of an object or an entire graph of objects. Data binding should deal with all necessary type conversion since request parameters, which are typically delivered by a form submission, are always strings whilst the properties of a Groovy or Java object may well not be.

Grails uses Spring's underlying data binding capability to perform data binding.

数据绑定是"绑定"输入的请求参数到一个对象的属性或者整个对象的行为。数据绑定将自动转换请求参数的类型,这些参数通常是通过表单提交来的String类型的值,而Groovy或者Java对象的属性很可能不是。

Grails是使用Spring的基本数据绑定能力来完成数据绑定。

Binding Request Data to the Model

There are two ways to bind request parameters onto the properties of a domain class. The first involves using a domain classes' Map constructor:

def save() {
    def b = new Book(params)
    b.save()
}

The data binding happens within the code new Book(params). By passing the params object to the domain class constructor Grails automatically recognizes that you are trying to bind from request parameters. So if we had an incoming request like:

/book/save?title=The%20Stand&author=Stephen%20King

Then the title and author request parameters would automatically be set on the domain class. You can use the properties property to perform data binding onto an existing instance:

def save() {
    def b = Book.get(params.id)
    b.properties = params
    b.save()
}

This has the same effect as using the implicit constructor.

绑定请求数据到领域模型

有两种方法将请求参数绑定到一个领域类的属性上。第一个是使用领域类的构造函数,只要参数类型是Map类型即可:

def save() {
    def b = new Book(params)
    b.save()
}

数据绑定是在new Book(params)代码中发生的。将参数params对象传递给到领域类的构造器时,Grails就可以自动识别你正在试图绑定来自请求中的参数。因此,假设我们有一个如下面所示的输入请求:

/book/save?title=The%20Stand&author=Stephen%20King

那么,titleauthor请求参数将自动被设置到领域类上。你也可以使用已经存在实例的properties属性来执行数据绑定:

def save() {
    def b = Book.get(params.id)
    b.properties = params
    b.save()
}

这与使用隐式构造函数是完全一样的

Data binding and Single-ended Associations

If you have a one-to-one or many-to-one association you can use Grails' data binding capability to update these relationships too. For example if you have an incoming request such as:

/book/save?author.id=20

Grails will automatically detect the .id suffix on the request parameter and look up the Author instance for the given id when doing data binding such as:

def b = new Book(params)

An association property can be set to null by passing the literal String "null". For example:

/book/save?author.id=null

单关联的数据绑定

如果你有一个one-to-one或者many-to-one关联,你也可以利用Grails的数据绑定能力来更新它们,比如:

/book/save?author.id=20

Grails将自动检测后缀为.id请求参数,并且在数据绑定的时候,会查找指定id的Author实例,比如:

def b = new Book(params)

一个关联属性也可以被设置为null,只要传给的String类型的"null"即可。比如:

/book/save?author.id=null

Data Binding and Many-ended Associations

If you have a one-to-many or many-to-many association there are different techniques for data binding depending of the association type.

If you have a Set based association (the default for a hasMany) then the simplest way to populate an association is to send a list of identifiers. For example consider the usage of <g:select> below:

<g:select name="books"
          from="${Book.list()}"
          size="5" multiple="yes" optionKey="id"
          value="${author?.books}" />

This produces a select box that lets you select multiple values. In this case if you submit the form Grails will automatically use the identifiers from the select box to populate the books association.

However, if you have a scenario where you want to update the properties of the associated objects the this technique won't work. Instead you use the subscript operator:

<g:textField name="books[0].title" value="the Stand" />
<g:textField name="books[1].title" value="the Shining" />

However, with Set based association it is critical that you render the mark-up in the same order that you plan to do the update in. This is because a Set has no concept of order, so although we're referring to books0 and books1 it is not guaranteed that the order of the association will be correct on the server side unless you apply some explicit sorting yourself.

This is not a problem if you use List based associations, since a List has a defined order and an index you can refer to. This is also true of Map based associations.

Note also that if the association you are binding to has a size of two and you refer to an element that is outside the size of association:

<g:textField name="books[0].title" value="the Stand" />
<g:textField name="books[1].title" value="the Shining" />
<g:textField name="books[2].title" value="Red Madder" />

Then Grails will automatically create a new instance for you at the defined position. If you "skipped" a few elements in the middle:

<g:textField name="books[0].title" value="the Stand" />
<g:textField name="books[1].title" value="the Shining" />
<g:textField name="books[5].title" value="Red Madder" />

Then Grails will automatically create instances in between. For example in the above case Grails will create 4 additional instances if the association being bound had a size of 2.

You can bind existing instances of the associated type to a List using the same .id syntax as you would use with a single-ended association. For example:

<g:select name="books[0].id" from="${bookList}"
          value="${author?.books[0]?.id}" />

<g:select name="books[1].id" from="${bookList}" value="${author?.books[1]?.id}" />

<g:select name="books[2].id" from="${bookList}" value="${author?.books[2]?.id}" />

Would allow individual entries in the books List to be selected separately.

Entries at particular indexes can be removed in the same way too. For example:

<g:select name="books[0].id"
          from="${Book.list()}"
          value="${author?.books[0]?.id}"
          noSelection="['null': '']"/>

Will render a select box that will remove the association at books0 if the empty option is chosen.

Binding to a Map property works the same way except that the list index in the parameter name is replaced by the map key:

<g:select name="images[cover].id"
          from="${Image.list()}"
          value="${book?.images[cover]?.id}"
          noSelection="['null': '']"/>

This would bind the selected image into the Map property images under a key of "cover".

多关联的数据绑定

如果你有一个one-to-many或者many-to-many的关联,那么根据关联类型的不同,将对应不同的数据绑定技术。

如果你使用基于Set的关联(hasMany缺省就是此种关联),那么最简单的方法就是传递一个标识符列表。比如下边<g:select>的用法:

<g:select name="books"
          from="${Book.list()}"
          size="5" multiple="yes" optionKey="id"
          value="${author?.books}" />

这将产生一个允许选择多个值的下拉框/选择框(select box)。上例中,如果你提交表单的话,Grails根据选择框的传来的标识符来自动关联books

即便如此,类似的情况下,你想使用此技术来更新关联对象饿属性,将行不通。不过你可以通过下标(subscript)操作符的方式来实现,比如:

<g:textField name="books[0].title" value="the Stand" />
<g:textField name="books[1].title" value="the Shining" />

此外,基于Set的关联还是有一个严重问题,那就是你要更新的内容总是以同样的顺序渲染的,这是因为Set本来就没有顺序的概念,虽然你可以通过books0books1来索引,但这并不能保证其关联顺序在服务器端也一致,当然你可以通过明确指定排序来比避免。

如果你使用基于List的关联的话,这并不是问题,因为List已经是有序的,并且可以通过索引进行引用。基于Map的关联也是。

还要注意的是,如果你正在绑定的关联是有大小的,最外侧的那个元素所在位置就是关联的大小:

<g:textField name="books[0].title" value="the Stand" />
<g:textField name="books[1].title" value="the Shining" />
<g:textField name="books[2].title" value="Red Madder" />

Grails将在你定义的位置自动创建一个实例。如果你中间“跳过”一些元素,比如:

<g:textField name="books[0].title" value="the Stand" />
<g:textField name="books[1].title" value="the Shining" />
<g:textField name="books[5].title" value="Red Madder" />

Grails将会自动创建中间跳过的实例。在上述示例中,关联的边界大小是2,Grails将在此基础上创建4个额外的实例。

你也可以使用和单关联那样的.id语法来绑定类型是List且已存在的关联,比如:

<g:select name="books[0].id" from="${bookList}"
          value="${author?.books[0]?.id}" />

<g:select name="books[1].id" from="${bookList}" value="${author?.books[1]?.id}" />

<g:select name="books[2].id" from="${bookList}" value="${author?.books[2]?.id}" />

将允许books List中各自独立的实体分别进行选择。

同样的方法也可以用来删除特定的索引元素,比如:

<g:select name="books[0].id"
          from="${Book.list()}"
          value="${author?.books[0]?.id}"
          noSelection="['null': '']"/>

将渲染一个可以删除关联books0的下拉框,当然只有选择项是空的时候才行。

绑定到Map属性的工作方式也是如此,不过要将参数名称中的列表索引替换为映射(map)的键(key):

<g:select name="images[cover].id"
          from="${Image.list()}"
          value="${book?.images[cover]?.id}"
          noSelection="['null': '']"/>

会将选择的图片通过键"cover"绑定到Map类型的images中。

Data binding with Multiple domain classes

It is possible to bind data to multiple domain objects from the params object.

For example so you have an incoming request to:

/book/save?book.title=The%20Stand&author.name=Stephen%20King

You'll notice the difference with the above request is that each parameter has a prefix such as author. or book. which is used to isolate which parameters belong to which type. Grails' params object is like a multi-dimensional hash and you can index into it to isolate only a subset of the parameters to bind.

def b = new Book(params.book)

Notice how we use the prefix before the first dot of the book.title parameter to isolate only parameters below this level to bind. We could do the same with an Author domain class:

def a = new Author(params.author)

多领域类的数据绑定

通过params对象,是有可能将将数据绑定到多个领域对象的。

比如下面的输入请求:

/book/save?book.title=The%20Stand&author.name=Stephen%20King

你将会注意到上述请求的差异,即每一个参数都有一个前缀,比如author.或者book. ,它们通常用来隔离参数所属的类型。Grails的params对象更象是一个多维的哈希表(hash),你可以单独地只绑定参数的一个子集。

def b = new Book(params.book)

请注意我们是如何利用book.title参数的前缀(第一个点以前部分)来隔离此领域的子参数的。同样我们也可以用于领域类Author

def a = new Author(params.author)

Data Binding and Action Arguments

Controller action arguments are subject to request parameter data binding. There are 2 categories of controller action arguments. The first category is command objects. Complex types are treated as command objects. See the Command Objects section of the user guide for details. The other category is basic object types. Supported types are the 8 primitives, their corresponding type wrappers and java.lang.String. The default behavior is to map request parameters to action arguments by name:

class AccountingController {

// accountNumber will be initialized with the value of params.accountNumber // accountType will be initialized with params.accountType def displayInvoice(String accountNumber, int accountType) { // … } }

For primitive arguments and arguments which are instances of any of the primitive type wrapper classes a type conversion has to be carried out before the request parameter value can be bound to the action argument. The type conversion happens automatically. In a case like the example shown above, the params.accountType request parameter has to be converted to an int. If type conversion fails for any reason, the argument will have its default value per normal Java behavior (null for type wrapper references, false for booleans and zero for numbers) and a corresponding error will be added to the errors property of the defining controller.

/accounting/displayInvoice?accountNumber=B59786&accountType=bogusValue

Since "bogusValue" cannot be converted to type int, the value of accountType will be zero, controller.errors.hasErrors() will be true, controller.errors.errorCount will be equal to 1 and controller.errors.getFieldError('accountType') will contain the corresponding error.

If the argument name does not match the name of the request parameter then the @grails.web.RequestParameter annotation may be applied to an argument to express the name of the request parameter which should be bound to that argument:

import grails.web.RequestParameter

class AccountingController {

// mainAccountNumber will be initialized with the value of params.accountNumber // accountType will be initialized with params.accountType def displayInvoice(@RequestParameter('accountNumber') String mainAccountNumber, int accountType) { // … } }

数据绑定和操作参数

控制器的操作参数是绑定请求参数的主题,目前主要有2大类的操作参数。第一大类是命令对象,复杂类型的都被看作命令对象,更多详细请看本手册的命令对象章节。另外一大类是基本的对象类型,所支持的类型包括8个原生类型及其对应的包装类和java.lang.String。缺省情况下,从请求参数到操作参数的映射是通过名称来完成的,比如:

class AccountingController {

// accountNumber will be initialized with the value of params.accountNumber // accountType will be initialized with params.accountType def displayInvoice(String accountNumber, int accountType) { // … } }

对于参数是原生类型和其包装类实例来说,在请求参数要绑定到操作参数以前,是要执行一个类型转换的,不过此转换是自动完成的。以上述示例为例,请求参数params.accountType必须要转换成int才行。如果此转换失败了,不管什么原因导致,此参数将根据普通的Java行为进行设置(包装类是null,布尔类型是false,数字类型是0),并且产生一个相应的错误信息到控制器的errors属性中。

/accounting/displayInvoice?accountNumber=B59786&accountType=bogusValue

因为"bogusValue"不能被转换为int,所以accountType的值为0 ,而controller.errors.hasErrors()将返回true,controller.errors.errorCount的数值是1,并且controller.errors.getFieldError('accountType')将包含其对应的出错信息。

如果参数名称跟请求参数的名称并不匹配,那么可以使用@grails.web.RequestParameter注解解决,只需要将要转换的请求参数名传递给注解即可:

import grails.web.RequestParameter

class AccountingController {

// mainAccountNumber will be initialized with the value of params.accountNumber // accountType will be initialized with params.accountType def displayInvoice(@RequestParameter('accountNumber') String mainAccountNumber, int accountType) { // … } }

Data binding and type conversion errors

Sometimes when performing data binding it is not possible to convert a particular String into a particular target type. This results in a type conversion error. Grails will retain type conversion errors inside the errors property of a Grails domain class. For example:

class Book {
    …
    URL publisherURL
}

Here we have a domain class Book that uses the java.net.URL class to represent URLs. Given an incoming request such as:

/book/save?publisherURL=a-bad-url

it is not possible to bind the string a-bad-url to the publisherURL property as a type mismatch error occurs. You can check for these like this:

def b = new Book(params)

if (b.hasErrors()) { println "The value ${b.errors.getFieldError('publisherURL').rejectedValue}" + " is not a valid URL!" }

Although we have not yet covered error codes (for more information see the section on Validation), for type conversion errors you would want a message from the grails-app/i18n/messages.properties file to use for the error. You can use a generic error message handler such as:

typeMismatch.java.net.URL=The field {0} is not a valid URL

Or a more specific one:

typeMismatch.Book.publisherURL=The publisher URL you specified is not a valid URL

数据绑定和类型转换错误

有时,在执行数据绑定时,可能不会把一个特殊的String转换到特殊的目标类型。这样,你就得到一个类型转换错误。Grails将类型转换错误保存到领域类的errors属性中。比如::

class Book {
    …
    URL publisherURL
}

此处,我们拥有一个领域类Book,它使用了类java.net.URL来存储URLs。现在假设请求如下:

/book/save?publisherURL=a-bad-url

要将字符串a-bad-url绑定到publisherURL属性是不可能的,因为发生了类型不匹配的错误。你可以象下面那样进行检查:

def b = new Book(params)

if (b.hasErrors()) { println "The value ${b.errors.getFieldError('publisherURL').rejectedValue}" + " is not a valid URL!" }

虽然我们还没有涉及到错误编码(更多信息请参考校验章节),但对于类型转换错误,还是推荐你使用grails-app/i18n/messages.properties文件来定义错误信息。你可以使用通用的错误消息处理,比如:

typeMismatch.java.net.URL=The field {0} is not a valid URL

或者更准确地指定:

typeMismatch.Book.publisherURL=The publisher URL you specified is not a valid URL

Data Binding and Security concerns

When batch updating properties from request parameters you need to be careful not to allow clients to bind malicious data to domain classes and be persisted in the database. You can limit what properties are bound to a given domain class using the subscript operator:

def p = Person.get(1)

p.properties['firstName','lastName'] = params

In this case only the firstName and lastName properties will be bound.

Another way to do this is is to use Command Objects as the target of data binding instead of domain classes. Alternatively there is also the flexible bindData method.

The bindData method allows the same data binding capability, but to arbitrary objects:

def p = new Person()
bindData(p, params)

The bindData method also lets you exclude certain parameters that you don't want updated:

def p = new Person()
bindData(p, params, [exclude: 'dateOfBirth'])

Or include only certain properties:

def p = new Person()
bindData(p, params, [include: ['firstName', 'lastName]])

Note that if an empty List is provided as a value for the include parameter then all fields will be subject to binding if they are not explicitly excluded.

数据绑定和安全

当从请求参数进行批量地更新领域类地属性时,你要当心,绝不允许让用户的恶意数据绑定到领域类,并且持久化到数据库中。你可以使用下标操作符来限制那些属性可以被绑定到给定的领域类:

def p = Person.get(1)

p.properties['firstName','lastName'] = params

此处,只有firstNamelastName属性将被绑定。

另外一种方法是使用命令对象来替代领域类进行数据绑定。或者使用更灵活的bindData方法来绑定。

bindData方法具有同样的数据绑定能力,不过可以是任意对象:

def p = new Person()
bindData(p, params)

bindData方法还可以排除那些你不想更新的某些参数/属性:

def p = new Person()
bindData(p, params, [exclude: 'dateOfBirth'])

或者仅仅包含某些属性:

def p = new Person()
bindData(p, params, [include: ['firstName', 'lastName]])

注意!如果include参数值是一个空的列表,并且没有指定排除的话,那么所有的字段属性将被绑定。

6.1.7 XML和JSON响应

Using the render method to output XML

Grails supports a few different ways to produce XML and JSON responses. The first is the render method.

The render method can be passed a block of code to do mark-up building in XML:

def list() {

def results = Book.list()

render(contentType: "text/xml") { books { for (b in results) { book(title: b.title) } } } }

The result of this code would be something like:

<books>
    <book title="The Stand" />
    <book title="The Shining" />
</books>

Be careful to avoid naming conflicts when using mark-up building. For example this code would produce an error:

def list() {

def books = Book.list() // naming conflict here

render(contentType: "text/xml") { books { for (b in results) { book(title: b.title) } } } }

This is because there is local variable books which Groovy attempts to invoke as a method.

使用render方法输出XML

Grails支持几种不同的方法来产生XML和JSON响应。第一个就是render方法。

render方法可以被传递一个代码块,在代码块中,使用标签生成器来构建XML:

def list() {

def results = Book.list()

render(contentType: "text/xml") { books { for (b in results) { book(title: b.title) } } } }

这段代码的结果将会像下面这样:

<books>
    <book title="The Stand" />
    <book title="The Shining" />
</books>

注意!要避免根标签生成器的命名冲突。比如,下面的代码将会产生一个错误:

def list() {

def books = Book.list() // naming conflict here

render(contentType: "text/xml") { books { for (b in results) { book(title: b.title) } } } }

这是因为Groovy的本地变量books试图被当作一个方法来调用。

Using the render method to output JSON

The render method can also be used to output JSON:

def list() {

def results = Book.list()

render(contentType: "text/json") { books = array { for (b in results) { book title: b.title } } } }

In this case the result would be something along the lines of:

[
    {title:"The Stand"},
    {title:"The Shining"}
]

The same dangers with naming conflicts described above for XML also apply to JSON building.

使用render方法输出JSON

render方法也可以被用来输出JSON:

def list() {

def results = Book.list()

render(contentType: "text/json") { books = array { for (b in results) { book title: b.title } } } }

上述示例的结果输出如下几行的所示:

[
    {title:"The Stand"},
    {title:"The Shining"}
]

注意!上面XML命名冲突的危险同样适用于JSON。

Automatic XML Marshalling

Grails also supports automatic marshalling of domain classes to XML using special converters.

To start off with, import the grails.converters package into your controller:

import grails.converters.*

Now you can use the following highly readable syntax to automatically convert domain classes to XML:

render Book.list() as XML

The resulting output would look something like the following::

<?xml version="1.0" encoding="ISO-8859-1"?>
<list>
  <book id="1">
    <author>Stephen King</author>
    <title>The Stand</title>
  </book>
  <book id="2">
    <author>Stephen King</author>
    <title>The Shining</title>
  </book>
</list>

An alternative to using the converters is to use the codecs feature of Grails. The codecs feature provides encodeAsXML and encodeAsJSON methods:

def xml = Book.list().encodeAsXML()
render xml

For more information on XML marshalling see the section on REST

自动XML编组(Marshalling)

Grails还支持将领域类自动编组为XML的用法,不过要借助于特定的转换器。

首先,要先在你的控制器中的导入包grails.converters

import grails.converters.*

现在,你可以使用如下易读性高的语法来将领域类自动转换成XML:

render Book.list() as XML

上述的输出结果看起来如下所示:

<?xml version="1.0" encoding="ISO-8859-1"?>
<list>
  <book id="1">
    <author>Stephen King</author>
    <title>The Stand</title>
  </book>
  <book id="2">
    <author>Stephen King</author>
    <title>The Shining</title>
  </book>
</list>

另外一种转换器的用法是使用Grails的编码(codecs)功能。此功能提供了encodeAsXMLencodeAsJSON方法:

def xml = Book.list().encodeAsXML()
render xml

更多关于XML编组信息请参考REST章节

Automatic JSON Marshalling

Grails also supports automatic marshalling to JSON using the same mechanism. Simply substitute XML with JSON:

render Book.list() as JSON

The resulting output would look something like the following:

[
    {"id":1,
     "class":"Book",
     "author":"Stephen King",
     "title":"The Stand"},
    {"id":2,
     "class":"Book",
     "author":"Stephen King",
     "releaseDate":new Date(1194127343161),
     "title":"The Shining"}
 ]

Again as an alternative you can use the encodeAsJSON to achieve the same effect.

自动JSON编组

Grails同样也支持自动JSON编组的功能,这跟XML机制完全相同,因此只需要简单地将XML替换为JSON即可:

render Book.list() as JSON

其输出结果看起来如下所示:

[
    {"id":1,
     "class":"Book",
     "author":"Stephen King",
     "title":"The Stand"},
    {"id":2,
     "class":"Book",
     "author":"Stephen King",
     "releaseDate":new Date(1194127343161),
     "title":"The Shining"}
 ]

跟上面一样,你也可以使用encodeAsJSON来达到相同的效果。

6.1.8 关于JSONBuilder

The previous section on on XML and JSON responses covered simplistic examples of rendering XML and JSON responses. Whilst the XML builder used by Grails is the standard XmlSlurper found in Groovy, the JSON builder is a custom implementation specific to Grails.

JSONBuilder and Grails versions

JSONBuilder behaves different depending on the version of Grails you use. For version below 1.2 the deprecated grails.web.JSONBuilder class is used. This section covers the usage of the Grails 1.2 JSONBuilder

For backwards compatibility the old JSONBuilder class is used with the render method for older applications; to use the newer/better JSONBuilder class set the following in Config.groovy:

grails.json.legacy.builder = false

在以前关于XML和JSON响应的章节中,我们曾经简单地涉猎到渲染XML和JSON响应的例子。而Grails的XML生成器是Groovy标准的 XmlSlurper ,JSON生成器是Grails自己实现的一个规范。

JSONBuilder和Grails版本

JSONBuilder的行为根据你使用的Grails版本而有所不同。对于版本低于1.2,使用的是可以被废弃的grails.web.JSONBuilder类。本节将涉及到Grails 1.2 JSONBuilder的用法。

因为要兼容以前版本的原因,旧的JSONBuilder类被用于旧应用的render方法;而要使用更新更好的JSONBuilder类,需要在Config.groovy中做如下设置:

grails.json.legacy.builder = false

Rendering Simple Objects

To render a simple JSON object just set properties within the context of the Closure:

render(contentType: "text/json") {
    hello = "world"
}

The above will produce the JSON:

{"hello":"world"}

渲染简单对象

要渲染简单的JSON对象,只需要在闭包的上下内设置属性即可:

render(contentType: "text/json") {
    hello = "world"
}

上述将产生如下JSON输出:

{"hello":"world"}

Rendering JSON Arrays

To render a list of objects simple assign a list:

render(contentType: "text/json") {
    categories = ['a', 'b', 'c']
}

This will produce:

{"categories":["a","b","c"]}

You can also render lists of complex objects, for example:

render(contentType: "text/json") {
    categories = [ { a = "A" }, { b = "B" } ]
}

This will produce:

{"categories":[ {"a":"A"} , {"b":"B"}] }

Use the special element method to return a list as the root:

render(contentType: "text/json") {
    element 1
    element 2
    element 3
}

The above code produces:

[1,2,3]

渲染JSON数组

要渲染一个对象的列表,只需要简单给其赋值一个列表即可:

render(contentType: "text/json") {
    categories = ['a', 'b', 'c']
}

这将输出:

{"categories":["a","b","c"]}

你也可以渲染复杂对象的列表,比如:

render(contentType: "text/json") {
    categories = [ { a = "A" }, { b = "B" } ]
}

这将输出:

{"categories":[ {"a":"A"} , {"b":"B"}] }

使用特定的element方法可以返回一个根范围的列表:

render(contentType: "text/json") {
    element 1
    element 2
    element 3
}

上述代码产生如下输出:

[1,2,3]

Rendering Complex Objects

Rendering complex objects can be done with Closures. For example:

render(contentType: "text/json") {
    categories = ['a', 'b', 'c']
    title = "Hello JSON"
    information = {
        pages = 10
    }
}

The above will produce the JSON:

{"categories":["a","b","c"],"title":"Hello JSON","information":{"pages":10}}

渲染复杂对象

渲染复杂对象要在闭包内完成,比如:

render(contentType: "text/json") {
    categories = ['a', 'b', 'c']
    title = "Hello JSON"
    information = {
        pages = 10
    }
}

上述将输出JSON:

{"categories":["a","b","c"],"title":"Hello JSON","information":{"pages":10}}

Arrays of Complex Objects

As mentioned previously you can nest complex objects within arrays using Closures:

render(contentType: "text/json") {
    categories = [ { a = "A" }, { b = "B" } ]
}

You can use the array method to build them up dynamically:

def results = Book.list()
render(contentType: "text/json") {
    books = array {
        for (b in results) {
            book title: b.title
        }
    }
}

复杂对象数组

如前面所提到的,你可以在闭包内使用嵌套的复杂对象来实现数组:

render(contentType: "text/json") {
    categories = [ { a = "A" }, { b = "B" } ]
}

你也可以使用array方法来动态地构建它们:

def results = Book.list()
render(contentType: "text/json") {
    books = array {
        for (b in results) {
            book title: b.title
        }
    }
}

Direct JSONBuilder API Access

If you don't have access to the render method, but still want to produce JSON you can use the API directly:

def builder = new JSONBuilder()

def result = builder.build { categories = ['a', 'b', 'c'] title = "Hello JSON" information = { pages = 10 } }

// prints the JSON text println result.toString()

def sw = new StringWriter() result.render sw

直接使用JSONBuilder API

如果你不能使用render方法,你还是可以通过直接使用API来产生JSON的:

def builder = new JSONBuilder()

def result = builder.build { categories = ['a', 'b', 'c'] title = "Hello JSON" information = { pages = 10 } }

// prints the JSON text println result.toString()

def sw = new StringWriter() result.render sw

6.1.9 上传文件

Programmatic File Uploads

Grails supports file uploads using Spring's MultipartHttpServletRequest interface. The first step for file uploading is to create a multipart form like this:

Upload Form: <br />
    <g:uploadForm action="upload">
        <input type="file" name="myFile" />
        <input type="submit" />
    </g:uploadForm>

The uploadForm tag conveniently adds the enctype="multipart/form-data" attribute to the standard <g:form> tag.

There are then a number of ways to handle the file upload. One is to work with the Spring MultipartFile instance directly:

def upload() {
    def f = request.getFile('myFile')
    if (f.empty) {
        flash.message = 'file cannot be empty'
        render(view: 'uploadForm')
        return
    }

f.transferTo(new File('/some/local/dir/myfile.txt')) response.sendError(200, 'Done') }

This is convenient for doing transfers to other destinations and manipulating the file directly as you can obtain an InputStream and so on with the MultipartFile interface.

上传文件编程

Grails通过Spring的MultipartHttpServletRequest接口来支持文件上传。第一步就是要为文件上传创建一个multipart的表单,比如:

Upload Form: <br />
    <g:uploadForm action="upload">
        <input type="file" name="myFile" />
        <input type="submit" />
    </g:uploadForm>

uploadForm标签在标准<g:form>标签的基础上添加了enctype="multipart/form-data"属性。

此后就可以采用多种方式来处理文件上传。其一就是直接使用Spring的MultipartFile

def upload() {
    def f = request.getFile('myFile')
    if (f.empty) {
        flash.message = 'file cannot be empty'
        render(view: 'uploadForm')
        return
    }

f.transferTo(new File('/some/local/dir/myfile.txt')) response.sendError(200, 'Done') }

此种方式适合于将文件传送到其他的目的地,直接使用获取到的InputStream来操作文件等,不过这都依赖于MultipartFile接口。

File Uploads through Data Binding

File uploads can also be performed using data binding. Consider this Image domain class:

class Image {
    byte[] myFile

static constraints = { // Limit upload file size to 2MB myFile maxSize: 1024 * 1024 * 2 } }

If you create an image using the params object in the constructor as in the example below, Grails will automatically bind the file's contents as a byte to the myFile property:

def img = new Image(params)

It's important that you set the size or maxSize constraints, otherwise your database may be created with a small column size that can't handle reasonably sized files. For example, both H2 and MySQL default to a blob size of 255 bytes for byte properties.

It is also possible to set the contents of the file as a string by changing the type of the myFile property on the image to a String type:

class Image {
   String myFile
}

文件上传和数据绑定

文件上传也可以通过数据绑定来完成。假设如下的Image领域类:

class Image {
    byte[] myFile

static constraints = { // Limit upload file size to 2MB myFile maxSize: 1024 * 1024 * 2 } }

如果你如下例所示那样,通过params的构造方法来创建一个图像(image),那么Grails将会自动地把文件内容转换为byte,并且绑定到myFile属性上:

def img = new Image(params)

非常重要地一点是你一定要设置size或者maxSize约束,否则的话,你的数据库可能会创建一个字段太少的列,这样就不能合理的处理文件。比如,对于属性类型是byte的,H2和MySQL缺省的blob大小是255字节。

你也可以将myFile属性的类型改成字符串来保存文件的内容:

class Image {
   String myFile
}

6.1.10 命令对象

Grails controllers support the concept of command objects. A command object is similar to a form bean in a framework like Struts, and they are useful for populating a subset of the properties needed to update a domain class. Or where there is no domain class required for the interaction, but you need features such as data binding and validation.

Grails控制器支持命令对象(command objects)的概念。一个命令对象类似于Struts中的一个表单(form)bean,当你想要更新领域类属性的一个子集的时候,或者虽然没有领域类,你还是需要数据绑定校验特性的时候,是非常有用的,

Declaring Command Objects

Command objects are typically declared in the same source file as a controller, directly below the controller class definition. For example:

class UserController {
    …
}

class LoginCommand { String username String password

static constraints = { username(blank: false, minSize: 6) password(blank: false, minSize: 6) } }

As this example shows, you can define constraints in command objects just like in domain classes.

声明命令对象

命令对象通常声明在同一个控制器的源文件中,并且直接位于控制器类的下面。比如:

class UserController {
    …
}

class LoginCommand { String username String password

static constraints = { username(blank: false, minSize: 6) password(blank: false, minSize: 6) } }

正如上例所示,你可以象领域类那样定义命令对象的约束

Using Command Objects

To use command objects, controller actions may optionally specify any number of command object parameters. The parameter types must be supplied so that Grails knows what objects to create, populate and validate.

Before the controller action is executed Grails will automatically create an instance of the command object class, populate its properties with by binding the request parameters, and validate the command object. For example:

class LoginController {

def login = { LoginCommand cmd -> if (cmd.hasErrors()) { redirect(action: 'loginForm') return }

// work with the command object data } }

When using methods instead of Closures for actions, you can specify command objects in arguments:

class LoginController {
    def login(LoginCommand cmd) {
        if (cmd.hasErrors()) {
            redirect(action: 'loginForm')
            return
        }

// work with the command object data } }

使用命令对象

为了使用命令对象,控制器可以随意指定任何数目的命令对象参数。参数的类型是必需的,因为Grails需要知道什么样的对象被创建,写入和验证。

在控制器的操作被执行之前,Grails将自动创建一个命令对象类的实例,用相应名字的请求参数写入到命令对象属性,并且验证它们,例如:

class LoginController {

def login = { LoginCommand cmd -> if (cmd.hasErrors()) { redirect(action: 'loginForm') return }

// work with the command object data } }

当操作是用方法来定义时 ,你可以将命令对象直接作为参数,比如:

class LoginController {
    def login(LoginCommand cmd) {
        if (cmd.hasErrors()) {
            redirect(action: 'loginForm')
            return
        }

// work with the command object data } }

Command Objects and Dependency Injection

Command objects can participate in dependency injection. This is useful if your command object has some custom validation logic uses Grails services:

class LoginCommand {

def loginService

String username String password

static constraints = { username validator: { val, obj -> obj.loginService.canLogin(obj.username, obj.password) } } }

In this example the command object interacts with the loginService bean which is injected by name from the Spring ApplicationContext.

命令对象和依赖注入

命令对象也支持依赖注入。这在你自定义校验的逻辑依赖Grails的服务时,非常有用:

class LoginCommand {

def loginService

String username String password

static constraints = { username validator: { val, obj -> obj.loginService.canLogin(obj.username, obj.password) } } }

在上述示例中,命令对象跟注入到Spring ApplicationContext中的loginService进行交互。

6.1.11 处理重复的表单提交

Grails has built-in support for handling duplicate form submissions using the "Synchronizer Token Pattern". To get started you define a token on the form tag:

<g:form useToken="true" ...>

Then in your controller code you can use the withForm method to handle valid and invalid requests:

withForm {
   // good request
}.invalidToken {
   // bad request
}

If you only provide the withForm method and not the chained invalidToken method then by default Grails will store the invalid token in a flash.invalidToken variable and redirect the request back to the original page. This can then be checked in the view:

<g:if test="${flash.invalidToken}">
  Don't click the button twice!
</g:if>

The withForm tag makes use of the session and hence requires session affinity or clustered sessions if used in a cluster.

Grails内置了对表单重复提交的处理,其使用的模式是“同步标志模式(Synchronizer Token Pattern)”。作为开始,你需要先在form标签中定义一个标志:

<g:form useToken="true" ...>

然后在你的控制器代码中使用withForm方法来处理那些有效和无效的请求:

withForm {
   // good request
}.invalidToken {
   // bad request
}

如果你只是使用withForm方法而没有连到invalidToken方法的话,Grails将缺省地存储无效的标志到flash.invalidToken变量中,并且将请求重定向到上一个原始页面。这样就可以在视图中检查了:

<g:if test="${flash.invalidToken}">
  Don't click the button twice!
</g:if>

withForm标签使用的是session,因此要求是兼容会话的或者支持集群的会话-如果在集群中使用的话。

6.1.12 简单类型转换器

Type Conversion Methods

If you prefer to avoid the overhead of Data Binding and simply want to convert incoming parameters (typically Strings) into another more appropriate type the params object has a number of convenience methods for each type:

def total = params.int('total')

The above example uses the int method, and there are also methods for boolean, long, char, short and so on. Each of these methods is null-safe and safe from any parsing errors, so you don't have to perform any additional checks on the parameters.

Each of the conversion methods allows a default value to be passed as an optional second argument. The default value will be returned if a corresponding entry cannot be found in the map or if an error occurs during the conversion. Example:

def total = params.int('total', 42)

These same type conversion methods are also available on the attrs parameter of GSP tags.

类型转换方法

如果你倾向于避免数据绑定的开销,而只想简单地将输入参数(通常是字符串)转换为另外更合适地类型,可以通过params对象提供的一些便利方法来实现:

def total = params.int('total')

上述示例使用的是int方法,除此之外还有booleanlongcharshort等方法。每一个方法都是空指针安全的(null-safe)和类型解析安全的,因此你也就不需要执行任何额外的参数检查了。

每一个转换方法都允许将一个缺省值传递给第二个可选参数。如果映射中没有找到对应的实体或者进行转换的时候出现了错误,此缺省值将被返回。比如:

def total = params.int('total', 42)

同样的这些转换方法也适合于GSP标签的attrs参数。

Handling Multi Parameters

A common use case is dealing with multiple request parameters of the same name. For example you could get a query string such as ?name=Bob&name=Judy.

In this case dealing with one parameter and dealing with many has different semantics since Groovy's iteration mechanics for String iterate over each character. To avoid this problem the params object provides a list method that always returns a list:

for (name in params.list('name')) {
    println name
}

处理多个重名参数

我们会经常碰到要处理多个请求参数名相同的情况。比如得到你得到一个内容是?name=Bob&name=Judy的查询串

这种情况下,处理一个参数和多个参数的语法是有些不同的,因为Groovy的String迭代是基于字符的。要避免此问题,可以使用params对象提供的list方法,此方法总是返回一个列表:

for (name in params.list('name')) {
    println name
}

6.1.13 异步请求处理

Grails support asynchronous request processing as provided by the Servlet 3.0 specification. To enable the async features you need to set your servlet target version to 3.0 in BuildConfig.groovy:

grails.servlet.version = "3.0"

With that done ensure you do a clean re-compile as some async features are enabled at compile time.

With a Servlet target version of 3.0 you can only deploy on Servlet 3.0 containers such as Tomcat 7 and above.

Grails支持Servlet 3.0规范的异步请求处理。要使异步功能生效,你需要在BuildConfig.groovy中设置servlet的版本为3.0:

grails.servlet.version = "3.0"

除此之外,你还需要一个干净的支持异步特性的编译环境来重新编译一下。

使用Servlet 3.0版本以后,你只能将应用部署于支持Servlet 3.0的容器中,比如Tomcat 7及其以上版本。

Asynchronous Rendering

You can render content (templates, binary data etc.) in an asynchronous manner by calling the startAsync method which returns an instance of the Servlet 3.0 AsyncContext. Once you have a reference to the AsyncContext you can use Grails' regular render method to render content:

def index() {
    def ctx = startAsync()
    ctx.start {
        new Book(title:"The Stand").save()
        render template:"books", model:[books:Book.list()]
        ctx.complete()
    }
}

Note that you must call the complete() method to terminate the connection.

异步渲染

你可以通过调用startAsync方法的方式进行异步的内容渲染(比如模板、二进制数据等),此方法的返回值是Servlet 3.0 AsyncContext的一个实例。一旦获取到了AsyncContext的引用,你就可以使用Grails的render方法来渲染内容了:

def index() {
    def ctx = startAsync()
    ctx.start {
        new Book(title:"The Stand").save()
        render template:"books", model:[books:Book.list()]
        ctx.complete()
    }
}

注意!你必须要调用complete()方法来中止此连接。

Resuming an Async Request

You resume processing of an async request (for example to delegate to view rendering) by using the dispatch method of the AsyncContext class:

def index() {
    def ctx = startAsync()
    ctx.start {
        // do working
        …
        // render view
        ctx.dispatch()
    }
}

恢复一个异步请求

你可以通过AsyncContext类的dispatch方法来恢复一个异步请求(比如将其代理到一个视图):

def index() {
    def ctx = startAsync()
    ctx.start {
        // do working
        …
        // render view
        ctx.dispatch()
    }
}