Groovy findAll 和每个标签问题

发布于 2024-08-17 01:36:34 字数 975 浏览 8 评论 0原文

我有以下域类:

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 技术交流群。

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

发布评论

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

评论(2

一百个冬季 2024-08-24 01:36:34

标记迭代对象列表,但它不会像 User.findAll(..) 那样查询数据库,

正确的代码是:

<g:each in="${User.findAll('from User as u where u.company=:company order by username', [company: companyInstance])}" var="userInstance">
    <li><g:link controller="role" action="show"
         id="${userInstance.id}">${userInstance.encodeAsHTML()}</g:link>
   </li>
</g:each>

If如果您绝对想使用 g:findAll,那么您首先需要构建用户列表,如下所示:

<% def users = User.listOrderByUsername()%>
<g:findAll in="${users}" expr="it.company == ${companyInstance}  "> 

我希望它有所帮助,

问候,

Fabien。

The <g:findAll> tag iterates over an object list but it does not query the DB as done with User.findAll(..)

The correct code is:

<g:each in="${User.findAll('from User as u where u.company=:company order by username', [company: companyInstance])}" var="userInstance">
    <li><g:link controller="role" action="show"
         id="${userInstance.id}">${userInstance.encodeAsHTML()}</g:link>
   </li>
</g:each>

If you absolutely want to use g:findAll, then you first need to build the list of users as following:

<% def users = User.listOrderByUsername()%>
<g:findAll in="${users}" expr="it.company == ${companyInstance}  "> 

I hope it helps,

Regards,

Fabien.

黑色毁心梦 2024-08-24 01:36:34

你想的更简单:${userInstance.company.encodeAsHTML()}

It's more simple that you think: ${userInstance.company.encodeAsHTML()}

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