grails 控制器渲染错误

发布于 2024-11-06 00:06:32 字数 606 浏览 1 评论 0原文

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 页面?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩饰不了的爱 2024-11-13 00:06:32

将控制器操作更改为

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:hasErrors bean="${personInstance}">
   <div class="errors">
      <g:renderErrors bean="${personInstance}" as="list" />
   </div>
</g:hasErrors>

考虑运行 grailsgenerate-all packagename.Person 并编辑生成的控制器和 GSP。当然,先把你的移开。

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.

染墨丶若流云 2024-11-13 00:06:32

您必须设置:

查看:

render view:"/example"

或:

render "${personInstance.name} successfully inserted"

You have to set either:

View:

render view:"/example"

Or:

render "${personInstance.name} successfully inserted"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文