Grails 控制器错误

发布于 2024-11-06 05:54:22 字数 1903 浏览 6 评论 0原文

我有这个 DomainClass:

package cm

class XXX{


    String city
    String website

    static constraints = {
     city(nullable: true)
     website(nullable: true)

    }

    static mapping = {
        id column:'xxx_id'
        city column: 'xxx_city'
        website column: 'xxx_website'
        table "xxx_XXX"
        version false

}
}

控制器:

class ConferenceController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [XXXInstanceList: XXX.list(params), XXXInstanceTotal: XXX.count()]
    }

    def save = {
        def XXXInstance= new XXX(params)
        if (!XXXInstance.save(flush: true)) {
            render view: 'add', model: [XXXInstance: XXXInstance]
            return
        }

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

}

和我的 add.gsp 页面:

<head>

  <title> xXXx</title>
  <meta name="layout" content="main2" />


</head>

...


<g:form controller="XXX" action="save">

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


    year
    <g:textField name="year" /><br>
    website
    <g:textField name="website" /><br>

    <g:submitButton name="save2" value="Save" />

     </g:form></div>
    </div>


              ...
</body>

使用当前代码,一切正常,但是当约束失败时,它会显示相应的错误,但写入的值消失了。我无法弄清楚这一点- 我尝试了很多方法,但我确信解决方案和我的问题一样简单 -.- 请帮忙。

I have this DomainClass:

package cm

class XXX{


    String city
    String website

    static constraints = {
     city(nullable: true)
     website(nullable: true)

    }

    static mapping = {
        id column:'xxx_id'
        city column: 'xxx_city'
        website column: 'xxx_website'
        table "xxx_XXX"
        version false

}
}

The Controller:

class ConferenceController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [XXXInstanceList: XXX.list(params), XXXInstanceTotal: XXX.count()]
    }

    def save = {
        def XXXInstance= new XXX(params)
        if (!XXXInstance.save(flush: true)) {
            render view: 'add', model: [XXXInstance: XXXInstance]
            return
        }

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

}

and my add.gsp page:

<head>

  <title> xXXx</title>
  <meta name="layout" content="main2" />


</head>

...


<g:form controller="XXX" action="save">

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


    year
    <g:textField name="year" /><br>
    website
    <g:textField name="website" /><br>

    <g:submitButton name="save2" value="Save" />

     </g:form></div>
    </div>


              ...
</body>

With this current code, everything works fine, but when a contraint is failed, it is showns the corresponding error but the written values are gone.. I cant figure this out - i'v tried a lot of things, but im sure the solution is as easy as my question -.- Please help.

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

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

发布评论

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

评论(1

生来就爱笑 2024-11-13 05:54:22

我认为您应该检查 YOUR_PROJECT\grails-app\i18n 文件夹中的 *.properties 文件。他们有翻译的定义。研究一下,如果需要的话可以参考文档,你就会明白Grails是如何进行翻译的。

取消控制器中以下行的注释:

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

I think you should check the *.properties file in YOUR_PROJECT\grails-app\i18n folder. They have the definition for translation. Investigate it for a time and refer to the document if need, you will understand how Grails perform translation.

And uncomment the following line in your controller:

//flash.message = "${message(code: 'default.created.message', args: [message(code: 'person.label', default: 'Person'), personInstance.id])}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文