【问题标题】:Call method from form to sendMails从表单调用方法到 sendMails
【发布时间】:2014-05-17 18:37:30
【问题描述】:

我想从表单向电子邮件地址列表发送电子邮件:

这是我的表格:

        <g:form url="[resource:contactInstance, action:'notify']">
            <!-- START FORM -->

            <fieldset class="form">

                <div
                    class="fieldcontain ${hasErrors(bean: contactInstance, field: 'firstName', 'error')} required">
                    <label for="firstName"> <g:message
                            code="contact.firstName.label" default="First Name" /> <span
                        class="required-indicator">*</span>
                    </label>
                    <g:textField name="firstName" required=""
                        value="${contactInstance?.firstName}" />
                </div>

                <div
                    class="fieldcontain ${hasErrors(bean: contactInstance, field: 'lastName', 'error')} required">
                    <label for="lastName"> <g:message
                            code="contact.lastName.label" default="Last Name" /> <span
                        class="required-indicator">*</span>
                    </label>
                    <g:textField name="lastName" required=""
                        value="${contactInstance?.lastName}" />
                </div>

                <div
                    class="fieldcontain ${hasErrors(bean: contactInstance, field: 'email', 'error')} required">
                    <label for="email"> <g:message code="contact.email.label"
                            default="Email" /> <span class="required-indicator">*</span>
                    </label>
                    <g:textField name="email" required=""
                        value="${contactInstance?.email}" />
                </div>

                <div
                    class="fieldcontain ${hasErrors(bean: contactInstance, field: 'description', 'error')} required">
                    <label for="description"> <g:message
                            code="contact.description.label" default="Description" /> <span
                        class="required-indicator">*</span>
                    </label>
                    <g:textField name="description" required=""
                        value="${contactInstance?.description}" />
                </div>


            </fieldset>

            <fieldset class="buttons">
                <g:submitButton name="create" class="notify"
                    value="${message(code: 'default.button.create.label', default: 'Create')}" />
            </fieldset>

            <!-- END FORM -->
        </g:form>

那是我的ContactController.groovy

@Secured(['permitAll'])
@Transactional(readOnly = true)
class ContactController {

    private static final log = LogFactory.getLog(this)

    static allowedMethods = [notify: "POST"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond Contact.list(params), model:[contactInstanceCount: Contact.count()]
    }

    /**
     * Sends a Mail to a specified list of email addresses
     * @param sender
     * @param receiver
     * @param templateName
     * @param params
     * @return
     */
    def void notify(Contact contactInstance) {

        log.info("call notify method")

        EmailSettings emailAddresses = new EmailSettings()

        if (emailAddresses.list() == null) {
            notFound()
            return
        }

        mailService.sendMail {

            log.info("send email to address list")

            multipart true
            from 
            to emailAddresses.list().toArray()
            subject "[Customer Notification]  " +  contactInstance.firstName + " " + contactInstance.lastName
            body contactInstance.description

        }

        request.withFormat {
            form {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'Contact.label', default: 'Contact'), contactInstance.id])
                redirect contactInstance
            }
            '*'{ respond contactInstance, [status: OK] }
        }
    }
}

我的问题是我的方法没有被调用。任何建议我应该改变什么。(除了我没有收到错误消息)

非常感谢您的回答!

PS.: 邮件插件的配置应该没问题,因为spring security UI plugin 有效!

更新

更改从通知方法中删除void 我收到此错误:

compiler.GrailsProjectWatcher File [C:\Users\User\GrailsWorkspace\TestApp\grails-app\controllers\com\TestApp\ContactController.groovy] changed. Applying changes to application.
.....compiler.GrailsProjectWatcher Compilation Error: startup failed:
C:\Users\User\GrailsWorkspace\TestApp\grails-app\controllers\com\TestApp\ContactController.groovy: -1: The return type of java.lang.Object notify() in com.TestApp.ContactController is incompatible with void notify() in java.lang.Object
. At [-1:-1]  @ line -1, column -1.
1 error

【问题讨论】:

    标签: grails groovy grails-2.0 gsp


    【解决方案1】:

    您不能从视图中调用方法。将notify 更改为操作。只需删除 void 表单 notify

    def notify(Contact contactInstance) {
    

    编辑.......................... .....................

    更改操作名称 notify 因为 notify method already exists in Object Class。我的 Intellij Idea 出错:

    Method notify() cannot override mthod notify() in java.lang.Object: override method is final.
    

    【讨论】:

    • 非常感谢您的回答!请查看我的更新,因为我收到错误消息...
    • 更改操作的名称,因为notify 方法已存在于父对象中。我的 Intellij Idea 出错:Method notify() cannot override mthod notify() in java.lang.Object: override method is final.
    • 谢谢很多很棒的答案!你帮了我很多!但是,我仍然遇到一个异常:No such property: mailService 知道如何解决它吗?
    • 在你的控制器def mailService中注入mailService服务。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    相关资源
    最近更新 更多