问题描述
class Person
{
static embedded = ['forSale']
布尔isSelling
House forSale
}
类别房屋
{
整数房间
}
我有一个numBedrooms的选择控件,如下所示:
< tr class =prop>
< td valign =topclass =name>
< label for =numBedrooms>< g:message code =person.numBedrooms.labeldefault =Num Bedrooms/>< / label>
< / td>
< td valign =topclass =value $ {hasErrors(bean:personInstance,field:'forSale.numBedrooms','errors')}>
noSelection =$ {['null':'选择一个号码...']}
from =$ {1..6}
/>
< / td>
< / tr>
请注意,我在select的fieldValue中使用了forSale.numBedrooms。我一直无法生成脚手架代码来查看它应该如何完成,因为由create-views生成的创建视图不包含forSale House对象中的字段的引用。
我还没有能够通过GSP访问任何复合字段的检查,所以这是一个猜测。在任何情况下,GSP页面都可以正确呈现,尽管这可能是因为我无法保存任何数据。
我将numBedrooms的值作为的URl查询字符串...
& numBedrooms = 2
当我这样做时,保存代码在我的控制器中默默无闻 - 至少没有任何东西会写入数据库。我已经打开几乎所有的调试日志记录,但我没有在日志中发现任何错误消息,尽管显然是有的。
如果我删除numBedrooms参数从查询字符串,然后我的保存进行正常,所以我猜测这是解决numBedrooms。
任何线索我做错了什么,我可以是否追查我的问题?
我所做的是 generate-all
为 House
域复制并粘贴GSP代码,并在完成后删除文件。我还发现,在我使用 House
域名的情况下,创建模板来编辑 House
域名更为明智
对于你的GSP你需要类似的东西(注意name属性)
< tr class =prop>
< td valign =topclass =name>
< label for =forSale.numBedrooms>< g:message code =house.numBedrooms.labeldefault =Num Bedrooms/>< / label>
< / td>
< td valign =topclass =value $ {hasErrors(bean:personInstance.forSale,field:'numBedrooms','errors')}>
noSelection =$ {['null' :'选择一个号码...']}
from =$ {1..6}
/>
< / td>
< / tr>
在你的参数字符串中,你需要 * forSale * .numBedrooms = 2
。这段代码可以和
person.properties = params
或 new Person(params)
一起使用。嵌入式指令仅告诉Hibernate将参数包含在同一个表中,但它们仍然是单独的Domain类。它可能会为域生成一个表,即使你永远不会使用它。
希望这有助于。
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?
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.
这篇关于如何在Grails GSP中保存复合字段值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!