简体   繁体   English

GRAILS g:每个问题

[英]GRAILS g:each problem

I cant get the g:each to work.我无法让 g:each 工作。 I am trying to iterate over anything but it never works = doesnt generate any html.我正在尝试迭代任何东西,但它永远不会起作用=不生成任何 html。

index.gsp索引.gsp

<g:each var="i" in="${userList}" controller="user">
          <li>Item ${i.name}</li>
</g:each>

userController.groovy用户控制器.groovy

class UserController {
    ...        
        def userList = {

            User.list()

        }
        ...
}

Then I have User.groovy filled with number of users..然后我有 User.groovy 填充用户数..

What am i supposed to write in in="${.....}" to iterate for example over users declared as User.groovy?我应该在 in="${.....}" 中写什么来迭代例如声明为 User.groovy 的用户? I have tried: User, users, User.list()...我试过:用户,用户,User.list()...

Thanks谢谢

EDIT:编辑:

Let's say i have a假设我有一个

def findOne {

 [users : User.findAllByNameLike("%Petr%")

}

in my UserCotroller.在我的用户控制器中。

How do i use g:each for it because我如何使用 g:each 因为

<g:each var="user" in="${findOne}"> 

won't do anything..什么都不会。。

In your example.在你的例子中。 userList is a Closure, so it's the action name, so I'm assuming you're accessing http://localhost:8080/appname/user/userList userList是一个闭包,所以它是动作名称,所以我假设您正在访问http://localhost:8080/appname/user/userList

If you return something from a controller action to be rendered in the GSP, it has to be in a Map , the "model".如果您从要在 GSP 中呈现的 controller 操作返回某些内容,则它必须位于Map中,即“模型”。 Each value in the map is exposed in the GSP using the map key as its name. map 中的每个值都在 GSP 中公开,使用 map 键作为其名称。 So the controller action corresponding to your GSP would be因此,与您的 GSP 对应的 controller 操作将是

def userList = {
    [users: User.list()]
}

and then you can iterate with然后你可以迭代

<g:each var="user" in="${users}">
    <li>Item ${user.name}</li>
</g:each>

The name doesn't matter - it just has to be the same in the model map as in the GSP.名称无关紧要 - 它只需要在 model map 中与 GSP 中相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM