Reputation: 6623
I have Spring beans defined in resources.groovy. And I can access them in controllers. I also have some GSPs in views that do NOT have controllers. I wonder how can I access beans in GSPs just like EL expressions in JSF?
For example, given a domain class like this:
class TestBean {
def name = "hello"
}
In spring/resources.groovy:
test(mydomain.TestBean) { bean -> bean.scope = 'session' }
In UrlMapping.groovy:
'/test'(view:'/test')
Then, in views/test.gsp:
${test.name}
But the above code would throw an exception because ${test} is null. So, how can I access TestBean in GSP without controller?
I am using Grails 2.2. Thanks!
Upvotes: 4
Views: 1818
Reputation: 2789
You can also create variable with your bean as value:
<g:set var="testBean" bean="test"/>
and then use (in your *.gsp):
${testBean.name}
It's a bit quicker than creating custom TagLib
.
Upvotes: 5
Reputation:
You can create a TagLib
to to that job, and just call in your gsp.
The TagLib
class MyTagLib {
static namespace = "my"
def test
def myTag = { attrs, body ->
out << test.name
}
}
The View
<my:myTag />
Upvotes: 4