如何在 Grails GSP 中保存复合字段值?
我有一个复合域对象,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我所做的是为
House
域生成所有,然后复制并粘贴 GSP 代码,并在完成后删除文件。我还发现,在稍后使用House
域的情况下,创建模板来编辑House
域会更明智。对于您的 GSP,您需要类似这样的内容(注意名称属性)
在您的参数字符串中,您需要
*forSale*.numBedrooms=2
。此代码将与person.properties = params
或new Person(params)
一起使用。嵌入的“指令”仅告诉 Hibernate 将参数包含在同一个表中,它们仍然是单独的域类。它可能会为该域生成一个表,即使您可能永远不会使用它。
希望这有帮助。
What I do is
generate-all
for theHouse
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 theHouse
domain in the case where I am using theHouse
domain later on.For you GSP you need something like this (Notice the name attribute)
In your param string you need
*forSale*.numBedrooms=2
. this code will work withperson.properties = params
ornew 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.