简体   繁体   中英

Groovy PageRenderer not rendering GSP

I have a Grails application with version: 2.2.4. I'm trying to render a gsp into a string parameter and send that to Mandrill template to be sent as a mail. The code is as follows

import grails.gsp.PageRenderer

SampleService{
    PageRenderer groovyPageRenderer
    def mailService

    def sampleSendMail(List<String> names){
        def view = groovyPageRenderer.render(view: '/mail/_sampleMail', model: [names: names])
        mailService.sendMandrillTemplate(view)
    }
}

The GSP looks as follows

<%@ page contentType="text/html" %>
<table>
   <th>Name</th>
      <g:each in="${names}" var="name" >
          <tr>${name}</tr>
      </g:each>
</table>

When I test this locally, it worked as expected. But when I tested this in Development Environment the "view" parameter is always empty.

Is there a specific reason for this? This issue baffles me, as I cannot think of a logical reason for this issue as it works in my local machine. Also this does not throw any error messages while it was executed in Development Environment, it simply returns an empty String.

Any insight on this will be helpful

EDIT-------

I'm using the Mandril Plugin (org.grails.plugins:mandrill:0.5). I'm using the Mandrill sendTemplate method inside the "sendMandrillTemplate" method. This is not an issue with that. Issue is when I render a gsp in a variable, it's empty in my development environment.

In fact there is a thread about similar issue: https://github.com/grails/grails-views/issues/140

We are using gsp templates for creating email content, so there is possible an ugly hack to make it working both on dev and prod environemnt:

import java.nio.file.Paths
import org.grails.io.support.ClassPathResource
def notificationsPath = Paths.get(new ClassPathResource("notifications/_welcome.gsp").getURI()).toString()
def f = new File(notificationsPath)
def text = f.getText()


def user = User.findByUsername('email@example.com')
def properties = [url:"https://example.pl"]
def binding =  [user: user, properties: properties]

def engine = new groovy.text.SimpleTemplateEngine()
def template = engine.createTemplate(text).make(binding)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM