2011-09-01 26 views
2

我需要将我的电子邮件模板存储在可以编辑的位置,而无需重新部署grails应用程序。我正在寻找一种方法来将这些模板渲染为基于绝对路径的邮件插件的视图。目前,“视图”和grails渲染方法都基于grails-app/views位置。Grails邮件插件:查看绝对路径

更新:现在,我打算使用SimpleTemplateEngine并将字符串输入正文。我仍然想知道是否有更多的“grails”方式来解决这个问题。

+0

我个人没有这样做,但如果你确定外化所有的GSP,我想你可以在你的Config中设置'grails.gsp.view.dir'。 – proflux

+0

谢谢,但我不想将所有这些外部化。 – Gregg

+0

为您的邮件功能创建一个插件,并在插件中使用'grails.gsp.view.dir',使用默认的主应用程序? – proflux

回答

1

还没有工作,但这个问题似乎没有得到很多答案,所以我不认为这里有太多分散注意力。

这是我在resources.groovy尝试:

mailMessageContentRenderer(gregg.stackoverflow.CustomPathMailMessageContentRenderer) { 
    groovyPagesTemplateEngine = ref('groovyPagesTemplateEngine') 
    groovyPagesUriService = ref('groovyPagesUriService') 
    grailsApplication = ref('grailsApplication') 
} 

如果你通过邮件的插件,有两个豆mailMessageBuidlerFactorymailMessageContentRenderer定义。基于other people have done我真的认为这会覆盖渲染器并让您自定义行为。当我在日志记录中查看mailService.mailMessageBuilderFactory.mailMessageContentRenderer的类时,它是自定义类,因此接线似乎正常工作。我创建了以下自定义呈现:

package gregg.stackoverflow 

    import org.codehaus.groovy.grails.commons.ConfigurationHolder 

    class CustomPathMailMessageContentRenderer extends grails.plugin.mail.MailMessageContentRenderer { 


    @Override 
    protected createTemplate(String templateName, String controllerName, String pluginName) { 
     def gspOverride = ConfigurationHolder.config.mailtemplate.gsp.dir 
     println "Custom one!" 
     if (!gspOverride) { 
      return super.createTemplate(templateName, controllerName, pluginName) 
     } 
     else { 

      def contextPath = gspOverride + templateName +".gsp" 

      def gspFile = new File(contextPath) 

      def template = groovyPagesTemplateEngine.createTemplate(gspFile) 

      if (!template) { 
       if (pluginName) { 
        throw new IllegalArgumentException("Could not locate email view ${templateName} in plugin [$pluginName]") 
       } else { 
        throw new IllegalArgumentException("Could not locate mail body ${templateName}. Is it in a plugin? If so you must pass the plugin name in the [plugin] variable") 
       } 
      } 

      return template 
     } 
    } 
} 

而且,我添加了一个属性Config.groovy指示称为mailtemplate.gsp.dir普惠制目录,将默认功能和自定义目录之间进行切换。

但我放在那里的println声明不显示。在这漫长的一天之后,我把这当作是一种干扰,所以有可能我的大脑被炸了,但是我今天没有时间弄清楚,所以很抱歉发布了非工作,非常糟糕的代码。希望你弄明白。

Upate

好了,因为我OCD一旦我开始在像这样的事情,并能活咖啡因和没有食物长时间,我得到了这方面的工作在午餐...之类的,大部分的方式...... :-)你必须使用GSP视图,但我不确定模板是否可行(我猜在你的GSP视图中包含模板也是行不通的)。所以sendMail调用看起来是这样的:

mailtemplate.gsp.dir=C:\\gsptest

和GSP称为C:\gsptest\test.gsp我是能够成功地发送自己从一个电子邮件:

mailService.sendMail { 
     to "[email protected]" 
     body (view:"/test", model:[name:"Chris"]) 
     // subject, from, etc still work 
} 
所有上面的代码和行

所以外在的GSP。