grails controllers render errors

2019-07-14 07:31发布

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>

What modifications do i have to make in both the controller and the gsp page to render the errors to the gsp page?

2条回答
孤傲高冷的网名
2楼-- · 2019-07-14 08:12

You have to set either:

View:

render view:"/example"

Or:

render "${personInstance.name} successfully inserted"
查看更多
Bombasti
3楼-- · 2019-07-14 08:21

Change the controller action to

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
}

and add this just before your <g:form>:

<g:hasErrors bean="${personInstance}">
   <div class="errors">
      <g:renderErrors bean="${personInstance}" as="list" />
   </div>
</g:hasErrors>

Consider running grails generate-all packagename.Person and editing the generated controller and GSPs. Of course move yours out of the way first.

查看更多
登录 后发表回答