将_form.gsp模板从Grails 2导入Grails3

时间:2018-03-15 17:20:33

标签: forms grails gsp scaffolding

我正在使用Grails 3.3.3并为我的域类生成视图。看到删除_form.gsp文件,我感到震惊!该表单基本上在create.gsp中动态呈现,如下所示:

<!DOCTYPE html>
<html>
    <head>
        <meta name="layout" content="main" />
        <g:set var="entityName" value="\${message(code: '${propertyName}.label', default: '${className}')}" />
        <title><g:message code="default.create.label" args="[entityName]" /></title>
    </head>
    <body>
        <a href="#create-${propertyName}" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content&hellip;"/></a>
        <div class="nav" role="navigation">
            <ul>
                <li><a class="home" href="\${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
                <li><g:link class="list" action="index"><g:message code="default.list.label" args="[entityName]" /></g:link></li>
            </ul>
        </div>
        <div id="create-${propertyName}" class="content scaffold-create" role="main">
            <h1><g:message code="default.create.label" args="[entityName]" /></h1>
            <g:if test="\${flash.message}">
            <div class="message" role="status">\${flash.message}</div>
            </g:if>
            <g:hasErrors bean="\${this.${propertyName}}">
            <ul class="errors" role="alert">
                <g:eachError bean="\${this.${propertyName}}" var="error">
                <li <g:if test="\${error in org.springframework.validation.FieldError}">data-field-id="\${error.field}"</g:if>><g:message error="\${error}"/></li>
                </g:eachError>
            </ul>
            </g:hasErrors>
            <g:form resource="\${this.${propertyName}}" method="POST">
                <fieldset class="form">
                    <g:render template="form"/>
                </fieldset>
                <fieldset class="buttons">
                    <g:submitButton name="create" class="save" value="\${message(code: 'default.button.create.label', default: 'Create')}" />
                </fieldset>
            </g:form>
        </div>
    </body>
</html>

我喜欢Grails 2中的实际表单模板,它分别列出了类的每个属性。自定义表单更容易(例如,在表单页面上添加下拉列表)。以下是Employee类(_form.gsp)的示例:

<%@ page import="com.djamware.Employee" %>



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

</div>

<div class="fieldcontain ${hasErrors(bean: employeeInstance, field: 'created', 'error')} required">
    <label for="created">
        <g:message code="employee.created.label" default="Created" />
        <span class="required-indicator">*</span>
    </label>
    <g:datePicker name="created" precision="day"  value="${employeeInstance?.created}"  />

</div>

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

</div>

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

</div>

<div class="fieldcontain ${hasErrors(bean: employeeInstance, field: 'salary', 'error')} required">
    <label for="salary">
        <g:message code="employee.salary.label" default="Salary" />
        <span class="required-indicator">*</span>
    </label>
    <g:field name="salary" value="${fieldValue(bean: employeeInstance, field: 'salary')}" required=""/>

</div>

现在是的,我可以从Grails 2项目中借用_form.gsp文件并随意编辑它,但是没有办法在Grails 3中生成表单吗?

编辑:此外,如果我无法生成表单,那么如何自定义字段(例如,更改日期字段的格式或组合两个字段值)?

1 个答案:

答案 0 :(得分:1)

  

...但是有没有办法在Grails 3中生成表单?

没有