groovy闭包参数
发布时间:2020-12-14 16:25:22 所属栏目:大数据 来源:网络整理
导读:以下使用grails邮件插件提供的sendMail方法的示例显示在 this book中. sendMail { to "foo@example.org" subject "Registration Complete" body view:"/foo/bar",model:[user:new User()]} 我理解{}中的代码是一个闭包,它作为参数传递给sendMail.我也明白,主
以下使用grails邮件插件提供的sendMail方法的示例显示在
this book中.
sendMail { to "foo@example.org" subject "Registration Complete" body view:"/foo/bar",model:[user:new User()] } 我理解{}中的代码是一个闭包,它作为参数传递给sendMail.我也明白,主题和正文都是方法调用. 我试图找出实现sendMail方法的代码是什么样的,我最好的猜测是这样的: MailService { String subject String recipient String view def model sendMail(closure) { closure.call() // Code to send the mail now that all the // various properties have been set } to(recipient) { this.recipient = recipient } subject(subject) { this.subject = subject; } body(view,model) { this.view = view this.model = model } } 这是合理的,还是我错过了什么?特别是,在闭包(to,subject,body)中调用的方法是否必须与sendMail属于同一类的成员? 谢谢, 解决方法
MailService.sendMail关闭委托:
MailMessage sendMail(Closure callable) { def messageBuilder = new MailMessageBuilder(this,mailSender) callable.delegate = messageBuilder callable.resolveStrategy = Closure.DELEGATE_FIRST callable.call() def message = messageBuilder.createMessage() initMessage(message) sendMail message return message } 例如,MailMessageBuilder的方法: void to(recip) { if(recip) { if (ConfigurationHolder.config.grails.mail.overrideAddress) recip = ConfigurationHolder.config.grails.mail.overrideAddress getMessage().setTo([recip.toString()] as String[]) } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |