如何将数据绑定到具有嵌套属性的命令对象? (非域对象)
我正在尝试将一些数据绑定到作为命令对象一部分的对象。尝试使用该对象时,该对象保持为空。也许我没有在 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
}
关于为什么“book.title”的绑定失败有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在绑定之前初始化它,例如:
Try to initialize it before binding, like:
只需快速刺一下即可。
表单字段名称可能应该是 book_title 而不是使用句点(不确定在控制器中处理时是否会出现问题)。
在控制器中,首先创建书籍模型,然后将其分配给您想要绑定的类。
BooksBindingCommand 是一个模型吗?因为我不确定你想要实现什么目标。
Just a quick stab at it.
The form field name should probably be book_title rather than using a period (not sure if it becomes a problem when handled in the controller).
In your controller, create your book model first, then assign it to the class you want it bound to.
Is the BooksBindingCommand a model? Because I'm not sure what you're trying to achieve.