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/