Access Spring beans in GSP without controller

grails, gsp, spring

Solution

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`.

Problem

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!

Original source