簡體   English   中英

Groovy PageRenderer 不呈現 GSP

[英]Groovy PageRenderer not rendering GSP

我有一個版本為 2.2.4 的 Grails 應用程序。 我正在嘗試將 gsp 呈現為字符串參數並將其發送到 Mandrill 模板以作為郵件發送。 代碼如下

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)
    }
}

GSP 如下所示

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

當我在本地測試時,它按預期工作。 但是當我在開發環境中對此進行測試時,“view”參數始終為空。

這有什么具體原因嗎? 這個問題讓我感到困惑,因為我想不出這個問題的邏輯原因,因為它在我的本地機器上工作。 此外,這在開發環境中執行時不會拋出任何錯誤消息,它只是返回一個空字符串。

對此的任何見解都會有所幫助

編輯 - - - -

我正在使用 Mandril 插件 (org.grails.plugins:mandrill:0.5)。 我在“sendMandrillTemplate”方法中使用了 Mandrill sendTemplate 方法。 這不是問題。 問題是當我在變量中呈現 gsp 時,它在我的開發環境中是空的。

事實上,有一個關於類似問題的線程: https : //github.com/grails/grails-views/issues/140

我們正在使用 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