grails控制器呈现错误

时间:2011-05-08 22:24:53

标签: grails controller render

class Person {
   String name
   Integer age
   String street
   String email
}

class PersonController {
    def save = {
        def personInstance = new Person(params)
        personInstance.save(flush:true)
    }
}

main.gsp

<g:form controller="person" action="save">
    <g:textField name="name" />
    <g:textField name="age" />
    <g:textField name="street" />
    <g:textField name="email" />
    <g:submitButton name="save" value="Save" />
</g:form>

我必须在控制器和gsp页面中进行哪些修改才能将错误呈现给gsp页面?

2 个答案:

答案 0 :(得分:4)

将控制器操作更改为

def save = {
   def personInstance = new Person(params)
   if (!personInstance.save(flush: true)) {
      render view: 'create', model: [personInstance: personInstance]
      return
   }

   flash.message = "${message(code: 'default.created.message', args: [message(code: 'person.label', default: 'Person'), personInstance.id])}"
   redirect action: 'show', id: personInstance.id
}

并在<g:form>

之前添加此内容
<g:hasErrors bean="${personInstance}">
   <div class="errors">
      <g:renderErrors bean="${personInstance}" as="list" />
   </div>
</g:hasErrors>

考虑运行grails generate-all packagename.Person并编辑生成的控制器和GSP。当然,首先要将你们放在一边。

答案 1 :(得分:0)

你必须设置:

查看:

render view:"/example"

或者:

render "${personInstance.name} successfully inserted"