grails - 在 View 定义中生成的实例

标签 grails model-view-controller groovy

grails 中的 generate-all 将与 View (CRUD) 一起生成一个 Controller ,我正在尝试了解每个生成的代码块的影响,现在我似乎无法在 index.gsp 中找到 {accountInstanceList} 的定义。

/views/account/index.gsp

        <g:each in="${accountInstanceList}" status="i" var="accountInstance">
            <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">

                <td><g:link action="show" id="${accountInstance.id}">${fieldValue(bean: accountInstance, field: "firstname")}</g:link></td>

                <td>${fieldValue(bean: accountInstance, field: "middlename")}</td>

                <td>${fieldValue(bean: accountInstance, field: "lastname")}</td>

                <td>${fieldValue(bean: accountInstance, field: "email")}</td>

                <td>${fieldValue(bean: accountInstance, field: "role")}</td>

            </tr>
        </g:each>

据我所知,上述实例的定义应包含在 Controller 中

/controller/.../AccountController.groovy
package ers

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

@Transactional(readOnly = true)
class AccountController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

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

    def show(Account accountInstance) {
        respond accountInstance
    }

    def create() {
        respond new Account(params)
    }

    @Transactional
    def save(Account accountInstance) {
        if (accountInstance == null) {
            notFound()
            return
        }

        if (accountInstance.hasErrors()) {
            respond accountInstance.errors, view:'create'
            return
        }

        accountInstance.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'account.label', default: 'Account'), accountInstance.id])
                redirect accountInstance
            }
            '*' { respond accountInstance, [status: CREATED] }
        }
    }

    def edit(Account accountInstance) {
        respond accountInstance
    }

    @Transactional
    def update(Account accountInstance) {
        if (accountInstance == null) {
            notFound()
            return
        }

        if (accountInstance.hasErrors()) {
            respond accountInstance.errors, view:'edit'
            return
        }

        accountInstance.save flush:true

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

    @Transactional
    def delete(Account accountInstance) {

        if (accountInstance == null) {
            notFound()
            return
        }

        accountInstance.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'Account.label', default: 'Account'), accountInstance.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'account.label', default: 'Account'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}

{accountInstanceList} 不在 Controller 的 index Action 中,它在哪里定义?是否也生成了隐藏类?

最佳答案

render 方法确定你试图传递给 View 的“事物”的类型并创建一个合适的名称,如果是域对象列表,它是 DomainNameInstanceList,请参见类 DefaultHtmlRenderer
另见 scaffollding文档部分

...the standard scaffold views expect model variables of the form
<propertyName>InstanceList for collections and <propertyName>Instance
for single instances

关于grails - 在 View 定义中生成的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43580523/

相关文章:

groovy - 如果警报关闭,如何不运行测试

json - Grails Java 8时间JSON简化

grails - 完全禁用javamelody grails插件

grails - 类权限不是域类或 GORM 尚未正确初始化或已关闭

java - 为什么 Spring 和 Struts MVC 为其 Controller 类实现单例模式?

java - 如何使用 eclipse 和 gradle 对 Java 项目运行 Spock 测试?

grails - 奇怪的记号

在发布期间指定内容类型时,Grails 无法解析请求

javascript - AngularJS 通知模型更改的 View

javascript - 重新初始化主干 View 后事件未触发