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

发布于 2024-12-28 18:30:58 字数 792 浏览 0 评论 0原文

我正在尝试将一些数据绑定到作为命令对象一部分的对象。尝试使用该对象时,该对象保持为空。也许我没有在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生生漫 2025-01-04 18:30:58

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

// the command object
class BooksBindingCommand {
   Book book = new Book()
}

Try to initialize it before binding, like:

// the command object
class BooksBindingCommand {
   Book book = new Book()
}
浅笑依然 2025-01-04 18:30:58

只需快速刺一下即可。

表单字段名称可能应该是 book_title 而不是使用句点(不确定在控制器中处理时是否会出现问题)。

<g:textField name="book_title" value="Lord Of the Rings"/><br>

在控制器中,首先创建书籍模型,然后将其分配给您想要绑定的类。

def create = {
  def mybook = new Book()
  mybook.title = params.book_title
  def binder = new BooksBindingCommand()
  binder.book = mybook
}

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).

<g:textField name="book_title" value="Lord Of the Rings"/><br>

In your controller, create your book model first, then assign it to the class you want it bound to.

def create = {
  def mybook = new Book()
  mybook.title = params.book_title
  def binder = new BooksBindingCommand()
  binder.book = mybook
}

Is the BooksBindingCommand a model? Because I'm not sure what you're trying to achieve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文