我的WebApplication中有一个类User,还有一个类Driver,它是User的子类。这是代码:
包elektrova
class User {
String username
String password
String firstName
String lastName
UserRole role
static belongsTo = [
hauler:Hauler
]
static hasMany = [
createdJobs:Job,
activities:UserActivityHistory
]
static mappedBy = [
createdJobs:'creationUser'
]
static mapping = { tablePerHierarchy false }
static constraints = {
username(blank:false, size:4..15,matches:/[\S]+/, unique:true)
password(blank:false, size:4..15,matches:/[\S]+/)
firstName(blank:false, nullable:false)
lastName(blank:false, nullable:false)
role(blank:false, nullable:false)
}
@Override
def encodeAsHTML(){
firstName + " " + lastName
}
}
class Driver extends User{
UserRole role = UserRole.DRIVER
static hasMany = [
assignedJobs:Job
]
static mappedBy = [assignedJobs:'assignedUser']
static constraints = {
}
}
这是在bootstrap中创建tesdata:
User dispo = new Driver(hauler: hauler, firstName: "Dis", lastName: "Po", password: "dispo", role: UserRole.ADMIN, username: "dispo" ).save(failOnError: true)
Driver driver = new Driver(hauler: hauler, firstName: "Kai", lastName: "Uwe", password: "kauw", role: UserRole.DRIVER, username: "kauw" ).save(failOnError: true)
Driver driver2 = new Driver(hauler: hauler, firstName: "Hans", lastName: "Juergen", password: "haju", role: UserRole.DRIVER, username: "haju" ).save(failOnError: true)
User admin = new User(hauler: hauler, firstName: "Ad", lastName: "Min", password: "admin", role: UserRole.ADMIN, username: "admin" ).save(failOnError: true)
User admin2 = new User(hauler: hauler, firstName: "Ad", lastName: "Min", password: "admin", role: UserRole.ADMIN, username: "admin2" ).save(failOnError: true)
show.gsp:
<%@ page import="elektrova.User"%>
<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="main">
<g:set var="entityName"
value="${message(code: 'user.label', default: 'User')}" />
<title><g:message code="default.show.label" args="[entityName]" /></title>
</head>
<body>
<g:render template="/navi/navbar" />
<a href="#show-user" class="skip" tabindex="-1"><g:message
code="default.link.skip.label" default="Skip to content…" /></a>
<div class="nav" role="navigation">
<ul>
<li><g:link class="list" action="index">
<g:message code="view.user.button.label.list" args="[entityName]" />
</g:link></li>
<li><g:link class="create" action="create">
<g:message code="view.user.button.label.create" args="[entityName]" />
</g:link></li>
</ul>
</div>
<div id="show-user" class="scaffold-show" role="main">
<h1>
<g:message code="default.show.label" args="[entityName]" />${userInstance.toString() }
</h1>
<g:if test="${flash.message}">
<div class="message" role="status">
${flash.message}
</div>
</g:if>
<ol class="property-list user">
<% System.out.println "user : " + userInstance %>
<g:if test="${userInstance?.hauler}">
<li class="fieldcontain"><span id="hauler-label"
class="property-label"><g:message code="hauler.label"
default="Hauler" /></span> <span class="property-value"
aria-labelledby="hauler-label"><g:link controller="hauler"
action="show" id="${userInstance?.hauler?.id}">
${userInstance?.hauler?.encodeAsHTML()}
</g:link></span></li>
</g:if>
<g:if test="${userInstance?.username}">
<li class="fieldcontain"><span id="username-label"
class="property-label"><g:message code="user.username.label"
default="Username" /></span> <span class="property-value"
aria-labelledby="username-label"><g:fieldValue
bean="${userInstance}" field="username" /></span></li>
</g:if>
<g:if test="${userInstance?.password}">
<li class="fieldcontain"><span id="password-label"
class="property-label"><g:message code="user.password.label"
default="Password" /></span> <span class="property-value"
aria-labelledby="password-label"><g:fieldValue
bean="${userInstance}" field="password" /></span></li>
</g:if>
<g:if test="${userInstance?.firstName}">
<li class="fieldcontain"><span id="firstName-label"
class="property-label"><g:message
code="user.firstName.label" default="First Name" /></span> <span
class="property-value" aria-labelledby="firstName-label"><g:fieldValue
bean="${userInstance}" field="firstName" /></span></li>
</g:if>
<g:if test="${userInstance?.lastName}">
<li class="fieldcontain"><span id="lastName-label"
class="property-label"><g:message code="user.lastName.label"
default="Last Name" /></span> <span class="property-value"
aria-labelledby="lastName-label"><g:fieldValue
bean="${userInstance}" field="lastName" /></span></li>
</g:if>
<g:if test="${userInstance?.role}">
<li class="fieldcontain"><span id="role-label"
class="property-label"><g:message code="user.role.label"
default="Role" /></span> <span class="property-value"
aria-labelledby="role-label"><g:fieldValue
bean="${userInstance}" field="role" /></span></li>
</g:if>
<g:if test="${userInstance?.createdJobs}">
<li class="fieldcontain"><span id="createdJobs-label"
class="property-label"><g:message
code="user.createdJobs.label" default="Created Jobs" /></span> <g:each
in="${userInstance.createdJobs}" var="c">
<span class="property-value" aria-labelledby="createdJobs-label"><g:link
controller="job" action="show" id="${c.id}">
${c?.encodeAsHTML()}
</g:link></span>
</g:each></li>
</g:if>
<g:if test="${userInstance?.createdJobs}">
<li class="fieldcontain"><span id="createdJobs-label"
class="property-label"><g:message
code="user.createdJobs.label" default="Created Jobs" /></span> <g:each
in="${userInstance.createdJobs}" var="c">
<span class="property-value" aria-labelledby="createdJobs-label"><g:link
controller="job" action="show" id="${c.id}">
${c?.encodeAsHTML()}
</g:link></span>
</g:each></li>
</g:if>
</ol>
<g:form url="[resource:userInstance, action:'delete']" method="DELETE">
<fieldset class="buttons">
<g:link class="edit" action="edit" resource="${userInstance}">
<g:message code="default.button.edit.label" default="Edit" />
</g:link>
<g:actionSubmit class="delete" action="delete"
value="${message(code: 'default.button.delete.label', default: 'Delete')}"
onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');" />
</fieldset>
</g:form>
</div>
控制器:
import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional
@Transactional(readOnly = true)
class UserController {
def beforeInterceptor = [action: this.&auth]
def auth = {
if(!session.user) {
redirect(controller:"login", action: "index")
}
}
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
boolean isAdmin
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
model:[userInstanceList: User.list(params),userInstanceCount: User.count()]
}
def show(User userInstance) {
println 'User: ' + userInstance
respond userInstance
}
def create() {
respond new User(params)
}
@Transactional
def save(User userInstance) {
if (userInstance == null) {
notFound()
return
}
if (userInstance.hasErrors()) {
respond userInstance.errors, view:'create'
return
}
userInstance.save flush:true
request.withFormat {
form {
flash.message = message(code: 'default.created.message', args: [message(code: 'userInstance.label', default: 'User'), userInstance.id])
redirect userInstance
}
'*' { respond userInstance, [status: CREATED] }
}
}
def edit(User userInstance) {
respond userInstance
}
@Transactional
def update(User userInstance) {
if (userInstance == null) {
notFound()
return
}
if (userInstance.hasErrors()) {
respond userInstance.errors, view:'edit'
return
}
userInstance.save flush:true
request.withFormat {
form {
flash.message = message(code: 'default.updated.message', args: [message(code: 'User.label', default: 'User'), userInstance.id])
redirect userInstance
}
'*'{ respond userInstance, [status: OK] }
}
}
@Transactional
def delete(User userInstance) {
if (userInstance == null) {
notFound()
return
}
userInstance.delete flush:true
request.withFormat {
form {
flash.message = message(code: 'default.deleted.message', args: [message(code: 'User.label', default: 'User'), userInstance.id])
redirect action:"index", method:"GET"
}
'*'{ render status: NO_CONTENT }
}
}
protected void notFound() {
request.withFormat {
form {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'userInstance.label', default: 'User'), params.id])
redirect action: "index", method: "GET"
}
'*'{ render status: NOT_FOUND }
}
}
}
现在一切正常在UserController中,正确的用户实例总是传递给视图。但是gsp对所有对象都接收null,除了两个管理员,它们被正确呈现。起初我认为它是因为视图无法处理子类,但Dispo用户也没有显示。更改其他实例的角色仍然不会显示它们。 有什么建议吗?
答案 0 :(得分:3)
这是一个古老的问题,但我遇到了同样的问题而且#34;修复了&#34;它通过向respond
语句添加模型参数:
def show(User userInstance) {
println 'User: ' + userInstance
respond userInstance, model:[userInstance:userInstance]
}
就我而言,&#34;用户&#34; class是一个基类,而我实际上是在基类控制器中处理它继承的类。 respond
完全被此感到困惑。