Groovy findAll 和每个标签问题
我有以下域类:
class User = {
String username
...
Company company
}
class Company {
String name
...
}
也就是说,用户和公司之间存在:1 关系。 这些课程就是这样,我无法改变它们。
在 show.gsp 上,我想获得公司的详细信息以及链接 属于该公司的用户。
我知道我可以通过编写自己的标签来实现此目的,但我确信使用 each 标签或 findAll 标签可以实现这一点。
如果我执行以下操作,
<g:each in="${User.findAll('from User order by username')}" var="userInstance">
<li><g:link controller="role" action="show"
id="${userInstance.id}">${userInstance.encodeAsHTML()}</g:link>
</li>
</g:each>
我尝试将 ${companyInstance} 作为参数传递,但要么出现异常,要么不起作用。
我还尝试过使用User.findAllByCompany。
使用时:
<g:findAll in="${user}" expr="it.company == ${companyInstance} ">
我得到的是空集。
有没有一种简单的方法可以在不编写标签库的情况下实现这一目标?
提前致谢。
路易斯
I have the following domain classes:
class User = {
String username
...
Company company
}
class Company {
String name
...
}
That is, there is a n:1 relationship between user and company.
These classes are so and I cannot change them.
At the show.gsp I want to have the details of the company together with links to
the users who belongs to this company.
I know that I can achieve this writing an own tag, but I am sure that this would be possible using the each tag or the the findAll tag.
If I do the following
<g:each in="${User.findAll('from User order by username')}" var="userInstance">
<li><g:link controller="role" action="show"
id="${userInstance.id}">${userInstance.encodeAsHTML()}</g:link>
</li>
</g:each>
I've tried to pass the ${companyInstance} as a parameter but either I got an exception or it didn't work.
I've also tried using User.findAllByCompany.
When using:
<g:findAll in="${user}" expr="it.company == ${companyInstance} ">
I am getting an empty set.
Is there an easy way to achieve this without writing a taglib?
Thanks in advance.
Luis
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标记迭代对象列表,但它不会像User.findAll(..)
那样查询数据库,正确的代码是:
If如果您绝对想使用 g:findAll,那么您首先需要构建用户列表,如下所示:
我希望它有所帮助,
问候,
Fabien。
The
<g:findAll>
tag iterates over an object list but it does not query the DB as done withUser.findAll(..)
The correct code is:
If you absolutely want to use g:findAll, then you first need to build the list of users as following:
I hope it helps,
Regards,
Fabien.
你想的更简单:
${userInstance.company.encodeAsHTML()}
It's more simple that you think:
${userInstance.company.encodeAsHTML()}