【问题标题】:how to call variable from config.groovy如何从 config.groovy 调用变量
【发布时间】:2015-06-17 13:40:20
【问题描述】:

我正在使用下面的代码来在 grails 中使用邮件插件

      try {
                CH.config.grails.mail.default.from = "${parent_personal_data.email}"
                mailSender.username = "${parent_personal_data.email}"
                mailSender.password = "${parent_data.password}"
                mailService.sendMail {
                to "${employee_personal_data.email}"
                subject "new task"
                body "you have been added to project and you are given a task"
          }
      }
      catch (Exception e) {
      // catch block code
      }
      // Set the original settings back
      finally {
          CH.config.grails.mail.default.from = defaultFrom
          mailSender.username = oldUsername
          mailSender.password = oldPassword
      }

它怎么一直向我显示错误 没有这样的财产:CH 没有这样的属性:mailSender

【问题讨论】:

    标签: grails gsp


    【解决方案1】:

    我不知道您的代码中的 CH 是什么,或者您正在使用什么版本的 grails,但获取配置元素的适当方法是通过 grailsApplicationHolders

    在控制器或服务中,您可以注入 grailsApplication。

    class SomeService {
      def grailsApplication
    
      def someMethod() {
        def from = grailsApplication.config.grails.mail.default.from
        ...
      }
    }
    

    或者您可以使用 grails.util.Holders 类。

    class SomeService {
      def grailsApplication
    
      def someMethod() {
        def from = Holders.config.grails.mail.default.from
        ...
      }
    }
    

    阅读一些docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多