一个简单的Grails形式。我不知所措

时间:2014-01-23 14:01:24

标签: forms grails

我将在前言中说我的背景是平面设计(已经做了几年的JavaScript)。好吧,我一直在寻找一个关于如何使用控制器创建基本表单的简单教程电子邮件正文中的表单信息。

这就是我在GSP中所拥有的:

<g:form name="batchform"   url="[action:'forminfo', controller:'onboardingform'] />
<label>Application Group or Area</label>
<g:textField name="apparea01" type="text" placeholder="Place Holder Text Field 1" value='${params.apparea01}' />
<label>Department Name</label>
<g:textField  name="deptname02" type="text" placeholder="Place Holder Text Field 2" value='${params.deptname02}' />

<input type="submit" value="Submit Form" id="FormButton" >
<g:form>

控制器是我感到困惑的地方:

package .com
import util.Environment
import MailTools
import MetricsLogger

class OnboardingformController {
        String apparea01,
    String deptname02

        static constraints = {
        apparea01 blank: false, nullable: false
        deptname02 blank: false, nullable: false
                }

         def index() {
        render (view:'onboardingform.gsp')

             }

         sendMail {
        to 'me@email.com'
        cc params.mail
        subject 'I am the form test in the subjectline'
        body 'i am the body of the form. I should have vars here that display the user input from form fields'

        }

}

我目前没有设置DOMAIN,因为此网站没有附加数据库。我只需要将这些字段输入推送到电子邮件正文中。我意识到这可能是一个让我失望的非常基本的操作(我的背景是美术,而不是计算机科学)。

MailTools已配置为默认邮箱。表单需要发送到另一个电子邮件地址。 MetricsLogger在登陆页面时捕获用户信息。

提前致谢。在我成为艺术家/程序员的过程中,这些知识将让我眼前一亮:)

1 个答案:

答案 0 :(得分:0)

好的,我在

中想出了这个控制器

grails-app/controllers/test/OnboardingformController.groovy

package test

class OnboardingformController {

    def index() { 
        render( view: 'onboardingForm.gsp' )
    }

    def formSubmission( String apparea01, String deptname02, String email ) {
        println "Send Mail from $email to me@email.com with apparea01=$apparea01 and deptname02=$deptname02"
        /*
        // Commented out for now so I don't have to set up mail ;-)
        sendMail {
            to 'me@email.com'
            cc params.mail
            subject 'I am the form test in the subjectline'
            body 'i am the body of the form. I should have vars here that display the user input from form fields'
        }
        */
        redirect( action:'index' )
    }
}

此GSP grails-app/onboardingform/onboardingform.gsp

<!DOCTYPE html>
<html>
    <head>
        <meta name="layout" content="main"/>
        <title>Onboarding</title>
    </head>
    <body>
        <g:form action="formSubmission">
            <p>
                <label>Application Group or Area</label>
                <g:textField name="apparea01" type="text" placeholder="Place Holder Text Field 1" value='${params.apparea01}' />
            </p>
            <p>
                <label>Department Name</label>
                <g:textField name="deptname02" type="text" placeholder="Place Holder Text Field 2" value='${params.deptname02}' />
            </p>
            <p>
                <label>Email</label>
                <g:textField name="email" type="text" placeholder="Place Holder Text Field 3" value='${params.email}' />
            </p>
            <p>
                <input type="submit" value="Submit Form" id="FormButton" >
            </p>
        </g:form>
    </body>
</html>

当填写表单时,应打印输入的值(到控制台)并重定向到表单。如果有任何事情没有意义,请告诉我: - )

如果需要验证(没有域对象),可以使用命令对象。这会将控制器更改为:

package test

class OnboardingformController {

    def index() { 
        render( view: 'onboardingForm.gsp' )
    }

    def formSubmission( SubmissionCommandObject cmd ) {
        if( !cmd.validate() ) {
            render( view: 'onboardingForm.gsp', model:[ data: cmd ] )
            return
        }
        else {
            println "Send Mail from $cmd.email to me@email.com with apparea01=$cmd.apparea01 and deptname02=$cmd.deptname02"
        }
        redirect( action:'index' )
    }

}

class SubmissionCommandObject {
    String apparea01
    String deptname02
    String email

    static constraints = {
        apparea01 blank: false, nullable: false
        deptname02 blank: false, nullable: false
        email blank: false, email: true
    }
}

观点:

<!DOCTYPE html>
<html>
    <head>
        <meta name="layout" content="main"/>
        <title>Onboarding</title>
    </head>
    <body>
        <g:hasErrors bean="${data}">
            <g:renderErrors bean="${data}" as="list" />
        </g:hasErrors>
        <g:form action="formSubmission">
            <fieldset>
                <p class="prop ${hasErrors(bean:data, field:'apparea01', 'errors')}">
                    <label>Application Group or Area</label>
                    <g:textField name="apparea01" type="text" placeholder="Place Holder Text Field 1" value='${data?.apparea01}' />
                </p>
                <p class="prop ${hasErrors(bean:data, field:'deptname02', 'errors')}">
                    <label>Department Name</label>
                    <g:textField name="deptname02" type="text" placeholder="Place Holder Text Field 2" value='${data?.deptname02}' />
                </p>
                <p class="prop ${hasErrors(bean:data, field:'email', 'errors')}">
                    <label>Email</label>
                    <g:textField name="email" type="text" placeholder="Place Holder Text Field 3" value='${data?.email}' />
                </p>
                <p>
                    <input type="submit" value="Submit Form" id="FormButton" >
                </p>
            </fieldset>
        </g:form>
    </body>
</html>

显然,这可能更漂亮,但足以满足这个简单的例子