我有一个用户控制器,主要是 CRUD,spring 工具套件可以为 crud 自动生成基本功能视图。
package rms
import org.springframework.dao.DataIntegrityViolationException
class UserController {
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
def index() {
redirect(action: "list", params: params)
}
def list(Integer max) {
params.max = Math.min(max ?: 10, 100)
[userInstanceList: User.list(params), userInstanceTotal: User.count()]
}
def create() {
[userInstance: new User(params)]
}
def save() {
def userInstance = new User(params)
if (!userInstance.save(flush: true)) {
render(view: "create", model: [userInstance: userInstance])
return
}
flash.message = message(code: 'default.created.message', args: [message(code: 'user.label', default: 'User'), userInstance.id])
redirect(action: "show", id: userInstance.id)
}
def show(Long id) {
def userInstance = User.get(id)
if (!userInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), id])
redirect(action: "list")
return
}
[userInstance: userInstance]
}
def edit(Long id) {
def userInstance = User.get(id)
if (!userInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), id])
redirect(action: "list")
return
}
[userInstance: userInstance]
}
def update(Long id, Long version) {
def userInstance = User.get(id)
if (!userInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), id])
redirect(action: "list")
return
}
if (version != null) {
if (userInstance.version > version) {
userInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
[message(code: 'user.label', default: 'User')] as Object[],
"Another user has updated this User while you were editing")
render(view: "edit", model: [userInstance: userInstance])
return
}
}
userInstance.properties = params
if (!userInstance.save(flush: true)) {
render(view: "edit", model: [userInstance: userInstance])
return
}
flash.message = message(code: 'default.updated.message', args: [message(code: 'user.label', default: 'User'), userInstance.id])
redirect(action: "show", id: userInstance.id)
}
def delete(Long id) {
def userInstance = User.get(id)
if (!userInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), id])
redirect(action: "list")
return
}
try {
userInstance.delete(flush: true)
flash.message = message(code: 'default.deleted.message', args: [message(code: 'user.label', default: 'User'), id])
redirect(action: "list")
}
catch (DataIntegrityViolationException e) {
flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'user.label', default: 'User'), id])
redirect(action: "show", id: id)
}
}
}
然后我尝试在我创建的不同 gsp 中使用它的方法,而不是在视图/用户下。我从自动生成的视图/用户/列表中复制了表格 html 和分页 html 代码。在我自己看来,其余的只是静态的 html 页眉和页脚,所以它不应该很重要。
<table>
<thead>
<tr>
<g:sortableColumn property="username" title="${message(code: 'user.username.label', default: 'Username')}" />
<g:sortableColumn property="password" title="${message(code: 'user.password.label', default: 'Password')}" />
<g:sortableColumn property="dateOfBirth" title="${message(code: 'user.dateOfBirth.label', default: 'Date Of Birth')}" />
<g:sortableColumn property="contactNumber" title="${message(code: 'user.contactNumber.label', default: 'Contact Number')}" />
<g:sortableColumn property="employeeID" title="${message(code: 'user.employeeID.label', default: 'Employee ID')}" />
<g:sortableColumn property="employeeStatus" title="${message(code: 'user.employeeStatus.label', default: 'Employee Status')}" />
</tr>
</thead>
<tbody>
<g:each in="${userInstanceList}" status="i" var="userInstance">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td><g:link action="show" id="${userInstance.id}">${fieldValue(bean: userInstance, field: "username")}</g:link></td>
<td>${fieldValue(bean: userInstance, field: "password")}</td>
<td><g:formatDate date="${userInstance.dateOfBirth}" /></td>
<td>${fieldValue(bean: userInstance, field: "contactNumber")}</td>
<td>${fieldValue(bean: userInstance, field: "employeeID")}</td>
<td>${fieldValue(bean: userInstance, field: "employeeStatus")}</td>
</tr>
</g:each>
</tbody>
</table>
<div class="pagination">
<g:paginate total="${userInstanceTotal}" />
</div>
每当我在自己的视图下使用可排序列时,它都会被重定向到视图/用户/列表,然后分页无法找到总计的值,即 userInstanceTotal。我自己的视图下也没有数据,但是在我使用 view/user/create 创建它之后,创建的数据正在自动生成的 CRUD 视图/用户/列表上工作