如何将数据绑定到具有嵌套属性的命令对象? (非域对象) [英] How to bind data to a command object that has a nested property? (non domain object)
问题描述
我试图将一些数据绑定到作为命令对象一部分的对象。该对象在尝试使用时保持为空。可能我没有在gsp中给出正确的数据,但我不知道我做错了什么!
我希望当我提交一个带有字段名称的表单'book.title'这将被映射到命令对象中,但是这会失败..标题保留[null]
每当我将命令对象和窗体更改为使用String title作为它的属性。
//提交数据的表单
< g:form> ;
< br>< br>
< g:actionSubmit action =createvalue =Create!/>
< / g:表格>
//控制器代码
def create = {BooksBindingCommand cmd - >
println cmd?.book?.title //书属性总是保留为空
重定向(action:index)
}
//命令对象
class BooksBindingCommand {
Book book
}
// book class,简单的普通groovy类
class Book {
String title
}
有关book.title绑定失败原因的任何建议?
尝试在绑定之前对其进行初始化,例如:
//命令对象
class BooksBindingCommand {
Book book = new Book()
}
I am trying to bind some data to an object that is part of a command object. The object stays null when trying to use it. Probably i am not giving the correct data in the gsp but i have no clue what am i doing wrong!
I would expect that when i submit a form with a field name 'book.title' this would get mapped into the command object.. but this fails.. The title stays [null]
Whenever i change the command object and form to use String title as property it works..
// 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
}
Any suggestion on why the binding of 'book.title' fails?
Try to initialize it before binding, like:
// the command object
class BooksBindingCommand {
Book book = new Book()
}
这篇关于如何将数据绑定到具有嵌套属性的命令对象? (非域对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!