如何将数据绑定到具有嵌套属性的命令对象?(非域对象)

Mar*_*rco 1 grails groovy command-objects

我试图将一些数据绑定到作为命令对象一部分的对象.尝试使用它时,该对象保持为null.可能我没有在gsp中给出正确的数据,但我不知道我做错了什么!

我希望当我提交一个带有字段名称'book.title'的表单时,这将被映射到命令对象..但是这会失败..标题保持[null]

每当我更改命令对象和表单以使用字符串标题作为属性它工作..

// the form that submits the data
<g:form>
   <g:textField name="book.title" value="Lord Of the Rings"/><br>
   <br><br>
   <g:actionSubmit action="create" value="Create!"/>
</g:form>


// the controller code
def create = { BooksBindingCommand cmd ->
   println cmd?.book?.title // the book property always stays null
   redirect(action: "index")
}

// the command object
class BooksBindingCommand {
   Book book
}

// the book class, simple plain groovy class
class Book {
   String title
}
Run Code Online (Sandbox Code Playgroud)

关于为什么'book.title'的绑定失败的任何建议?

Igo*_*nov 7

尝试在绑定之前初始化它,例如:

// the command object
class BooksBindingCommand {
   Book book = new Book()
}
Run Code Online (Sandbox Code Playgroud)