GRAILS g:each problem
I cant get the g:each to work. I am trying to iterate over anything but it never works = doesnt generate any html.
index.gsp
<g:each var="i" in="${userList}" controller="user">
<li>Item ${i.name}</li>
</g:each>
userController.groovy
class UserContro开发者_StackOverflow社区ller {
...
def userList = {
User.list()
}
...
}
Then I have User.groovy filled with number of users..
What am i supposed to write in in="${.....}" to iterate for example over users declared as User.groovy ? I have tried : User, users, 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 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
If you return something from a controller action to be rendered in the GSP, it has to be in a Map
, the "model". Each value in the map is exposed in the GSP using the map key as its name. So the controller action corresponding to your GSP would be
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.
精彩评论