简体   繁体   English

Groovy PageRenderer 不呈现 GSP

[英]Groovy PageRenderer not rendering GSP

I have a Grails application with version: 2.2.4.我有一个版本为 2.2.4 的 Grails 应用程序。 I'm trying to render a gsp into a string parameter and send that to Mandrill template to be sent as a mail.我正在尝试将 gsp 呈现为字符串参数并将其发送到 Mandrill 模板以作为邮件发送。 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 GSP 如下所示

<%@ 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.但是当我在开发环境中对此进行测试时,“view”参数始终为空。

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).我正在使用 Mandril 插件 (org.grails.plugins:mandrill:0.5)。 I'm using the Mandrill sendTemplate method inside the "sendMandrillTemplate" method.我在“sendMandrillTemplate”方法中使用了 Mandrill sendTemplate 方法。 This is not an issue with that.这不是问题。 Issue is when I render a gsp in a variable, it's empty in my development environment.问题是当我在变量中呈现 gsp 时,它在我的开发环境中是空的。

In fact there is a thread about similar issue: https://github.com/grails/grails-views/issues/140事实上,有一个关于类似问题的线程: 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:我们正在使用 gsp 模板来创建电子邮件内容,因此可能有一个丑陋的 hack 使其在开发和生产环境中都能正常工作:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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