(Quick Reference)

6.1.10 命令对象 - Reference Documentation

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

Version: null

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进行交互。