从表单到sendMails的调用方法

时间:2014-05-17 18:37:30

标签: grails groovy grails-2.0 gsp

我想将表单中的电子邮件发送到电子邮件地址列表:

这是我的形式:

        <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有效!

更新

从notify方法更改删除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

1 个答案:

答案 0 :(得分:1)

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

即可
def notify(Contact contactInstance) {

修改........................................... .................................

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

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