I have the following Grails 2.3.6 controller:
@Slf4j
class FizzController {
def index() {
List<Widget> widgets = getSomehow()
log.info("About to render foo.gsp with ${widgets.size()} widgets.")
render (
view: "foo",
model: widgets
)
}
}
Where foo.gsp
is:
<!DOCTYPE html>
<html>
<head>
<omitting a bunch of stuff for brevity>
</head>
<body>
<h3>Widget List:</h3>
<g:each in="${widgets}" var="widget" >
<h2>Name: ${widget.name}, Type: ${widget.type}</h3>
</g:each>
</body>
</html>
When I do a Grails run-app
and navigate my browser to:
http://localhost:8080/myapp/fizz
I see my foo
GSP view/page (with the <h3>
header tag), however it doesn't display anything else. When I view the page source code, I see:
<div id="page-body">
<h3>Widget List:</h3>
</div>
This indicates that I am injecting an empty List<Widget>
, right?
However in my log output, I see:
About to render foo.gsp with 1 widgets.
So it actually seems like I do have a non-empty List<Widget>
, but apparently I'm not properly injecting it into my GSP. So what's going on here?
you are passing the wrong model, it should be a map:
List<Widget> widgets = getSomehow()
render (
view: "foo",
model:[ widgets:widgets ]
)
then you can access it in your gsp:
<g:each in="${widgets}" var="widget" >
<h2>Name: ${widget.name}, Type: ${widget.type}</h3>
</g:each>