如何在 Grails GSP 中保存复合字段值?

发布于 2024-08-18 17:35:56 字数 1281 浏览 6 评论 0原文

我有一个复合域对象,如下所示:

class Person 
{
    static embedded = ['forSale']
    Boolean isSelling
    House forSale
}

class House 
{
    Integer numBedrooms
}

我有一个 numBedrooms 的选择控件,如下所示:

<tr class="prop">
 <td valign="top" class="name">
   <label for="numBedrooms"><g:message code="person.numBedrooms.label" default="Num Bedrooms" /></label>
 </td>
 <td valign="top" class="value ${hasErrors(bean: personInstance, field: 'forSale.numBedrooms', 'errors')}">
     <g:select name="numBedrooms" value="${fieldValue(bean: personInstance, field: 'forSale.numBedrooms')}" 
          noSelection="${['null':'Select a number...']}"
      from="${1..6}"
     />
 </td>
</tr>

请注意,我在选择的 fieldValue 中使用 forSale.numBedrooms。我无法为此生成脚手架代码来查看它应该如何完成,因为由 create-views 生成的创建视图不包含对 forSale House 对象中的字段的引用。

我也无法找到通过 GSP 访问复合字段的任何示例,因此这只是一个猜测。在任何情况下,GSP 页面都不会出现错误,尽管这可能是因为我无法保存任何数据。

我将 numBedrooms 的值作为 URl 查询字符串的一部分发送回来...

&numBedrooms=2

当我执行此操作时,控制器中的保存代码会默默失败 - 至少没有任何内容写入数据库。我已经打开了几乎所有内容的调试日志记录,但我在日志中没有收到表明有任何问题的消息,尽管显然有些问题。

如果我从查询字符串中删除 numBedrooms 参数,那么我的保存将正常进行,所以我猜测这与解析 numBedrooms 有关。

知道我做错了什么以及我可以做些什么来找出我的问题吗?

I have a composite domain object as follows:

class Person 
{
    static embedded = ['forSale']
    Boolean isSelling
    House forSale
}

class House 
{
    Integer numBedrooms
}

I have a select control for the numBedrooms as follows:

<tr class="prop">
 <td valign="top" class="name">
   <label for="numBedrooms"><g:message code="person.numBedrooms.label" default="Num Bedrooms" /></label>
 </td>
 <td valign="top" class="value ${hasErrors(bean: personInstance, field: 'forSale.numBedrooms', 'errors')}">
     <g:select name="numBedrooms" value="${fieldValue(bean: personInstance, field: 'forSale.numBedrooms')}" 
          noSelection="${['null':'Select a number...']}"
      from="${1..6}"
     />
 </td>
</tr>

Notice that I am using forSale.numBedrooms in the fieldValue on the select. I haven't been able to produce scaffolded code for this to take a look at how it is supposed to be done because the create view which gets generated by create-views contains no references to the fields in the forSale House object.

I also haven't been able to turn up any exampes of composite fields being accessed via GSP, so this is a bit of a guess. In any case the GSP page renders without errors, although that may be because I haven't been able to save any data.

I send the value of numBedrooms back as part of a URl query string...

&numBedrooms=2

When I do this the save code in my controller is failing silently - at least nothing ever gets written to the database. I have switched on debug logging for pretty much everything but I get no messages in the log which suggest anything is wrong, although something obviously is.

If I remove the numBedrooms parameter from the query string then my save proceeds as normal, so I am guessing it is something to do with resolving numBedrooms.

Any clue what I am doing wrong and what I can do to track down my problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

倾城°AllureLove 2024-08-25 17:35:56

我所做的是为 House 域生成所有,然后复制并粘贴 GSP 代码,并在完成后删除文件。我还发现,在稍后使用 House 域的情况下,创建模板来编辑 House 域会更明智。

对于您的 GSP,您需要类似这样的内容(注意名称属性)

<tr class="prop">
 <td valign="top" class="name">
   <label for="forSale.numBedrooms"><g:message code="house.numBedrooms.label" default="Num Bedrooms" /></label>
 </td>
 <td valign="top" class="value ${hasErrors(bean: personInstance.forSale, field: 'numBedrooms', 'errors')}">
     <g:select name="forSale.numBedrooms" value="${fieldValue(bean: personInstance.forSale, field: 'numBedrooms')}" 
          noSelection="${['null':'Select a number...']}"
      from="${1..6}"
     />
 </td>
</tr>

在您的参数字符串中,您需要 *forSale*.numBedrooms=2。此代码将与 person.properties = paramsnew Person(params) 一起使用。

嵌入的“指令”仅告诉 Hibernate 将参数包含在同一个表中,它们仍然是单独的域类。它可能会为该域生成一个表,即使您可能永远不会使用它。

希望这有帮助。

What I do is generate-all for the House Domain then copy and paste the GSP code and remove the files once I am done. I have also found it smarter to create templates to edit the House domain in the case where I am using the House domain later on.

For you GSP you need something like this (Notice the name attribute)

<tr class="prop">
 <td valign="top" class="name">
   <label for="forSale.numBedrooms"><g:message code="house.numBedrooms.label" default="Num Bedrooms" /></label>
 </td>
 <td valign="top" class="value ${hasErrors(bean: personInstance.forSale, field: 'numBedrooms', 'errors')}">
     <g:select name="forSale.numBedrooms" value="${fieldValue(bean: personInstance.forSale, field: 'numBedrooms')}" 
          noSelection="${['null':'Select a number...']}"
      from="${1..6}"
     />
 </td>
</tr>

In your param string you need *forSale*.numBedrooms=2. this code will work with person.properties = params or new Person(params).

The embedded "instruction" only tells Hibernate to include the parameters in the same table they are still seperate Domain classes. It will probably generate a table for the domain even though you may never use it.

Hope this helps.

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