3

I've created a Grails 3.2.1 app with rest-api profile and want to render a GSP template as a String. For this I first added, apply plugin:"org.grails.grails-gsp" in build.gradle then based on the code of ResponseRenderer, I tried following to render my GSP template as:

def viewResolver = applicationContext.getBean(CompositeViewResolver.BEAN_NAME, CompositeViewResolver)

View view = viewResolver.resolveView("/email-templates/signup", null)

But the view object is always null. Then I tried the following as well without success:

[ViewResolverComposite, GroovyMarkupViewResolver, GenericGroovyTemplateViewResolver].each {
    def viewResolver = applicationContext.getBean(it)

    View view = viewResolver.resolveViewName("/email-templates/signup", null)
    println view?.dump()
}

My template is located at grails-app/views/emails-templates/_signup.gsp.

In Grails 2, it was quite simple by injecting groovyPageRenderer bean of type PageRenderer but I think, that bean and class is no longer used in Grails 3.

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
  • 1
    Everything is still there, the `PageRenderer` class is in the grails-web-gsp jar. I assume the problem is that adding the `grails-gsp` plugin doesn't provide enough of the needed classpath. See if adding `compile 'org.grails:grails-web-gsp:3.2.1'` helps – Burt Beckwith Oct 23 '16 at 13:18
  • No @BurtBeckwith, adding that dependency didn't help – Shashank Agrawal Oct 23 '16 at 13:39
  • @BurtBeckwith After adding the dependency, the `groovyPageRenderer` was still not available as a bean in the context but I created a fresh 3.2.1 app with web profile and the `groovyPageRenderer` was there. The doc says about the rest profile: **Defaults to using JSON views for rendering responses (see the next section)** Any idea? Need to compare the `build.config`. – Shashank Agrawal Oct 23 '16 at 13:55
  • So I searched the `groovyPageRenderer` in GitHub code and found out that the bean is being registered in the [grails-plugin-gsp](https://github.com/grails/grails-core/blob/246b7264e8a638ada188ddba7a7a8812ba153399/grails-plugin-gsp/src/main/groovy/org/grails/plugins/web/GroovyPagesGrailsPlugin.groovy) plugin – Shashank Agrawal Oct 23 '16 at 14:01

2 Answers2

6

For Grails 3 app created with web profile, they can always use the groovyPageRenderer as given in the link Grails render view from service?.

And for the app created with rest-api profile, they just need to add the following in build.gradle under dependencies block:

compile "org.grails:grails-plugin-gsp:3.2.1"

And the above link will also work as is.

Just for the sake of description, inject the bean and use the render method:

PageRenderer groovyPageRenderer

def someMethod() {
    println groovyPageRenderer.render(template: "/email-templates/signup", model: [foo: "bar"])
}
Community
  • 1
  • 1
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
5

For Grails 3.3.x, few plugins have been externalized and artefact ids has changed.

Please refer here.

So, to make sure that groovyPageRenderer bean is registered, add

compile "org.grails.plugins:gsp"

to the dependencies block in build.gradle instead of

compile "org.grails:grails-plugin-gsp"

Just to make sure the plugin is initialised successfully in your app, add

logger('grails.plugins', TRACE)

in the logback.groovy file. You should see something like this in the console logs.

Grails plug-in [groovyPages] with version [3.3.x] loaded successfully

For compiling GSP files and adding it to the war/jar package, add

apply plugin:"org.grails.grails-gsp"

to your build.gradle file.

I hope this helps.

Nikhil Sharma
  • 891
  • 1
  • 8
  • 11