简化一切它应该看起来像这样。
服务方式:
def createContent (fileName, description) {
//creating an object to save
def content = new Content(
fileName:fileName,
description:description,
)
//saving the object
//if saved then savedContent is saved domain with generated id
//if not saved then savedContent is null and content has validation information inside
def savedContent = content.save()
if (savedContent != null) {
return savedContent
} else {
return content
}
}
现在在控制器中:
def someAction = {
...
def content = someService.createContent (fileName, description)
if (content.hasErrors()) {
//not saved
//render create page once again and use content object to render errors
render(view:'someAction', model:[content:content])
} else {
//saved
//redirect to show page or something
redirect(action:'show', model:[id:content.id])
}
}
还有一些Action.gsp:
<g:hasErrors bean="${content}">
<g:renderErrors bean="${content}" as="list" />
</g:hasErrors>
一般来说,您应该仔细阅读:Grails validation doc