【问题标题】:How render custom error messages?如何呈现自定义错误消息?
【发布时间】:2012-08-07 18:20:23
【问题描述】:

我有一个域类:

package x

class User {

    transient springSecurityService

    String username
    String password
        //other stuffs.
    static constraints = {
    username blank: false, unique: true
    password blank: false
    email blank: false, email:true
    }
}

这是我用于 Spring 安全性的类。在我的/register/index 页面中,我需要自定义错误消息,所以我将这些行添加到message.properties

x.User.username.unique=Username already exists. Please use other username.

但这似乎不起作用。我只收到此错误消息: Property [{0}] of class [{1}] cannot be null

即使我将一些值传递到我的username 列中,我仍然会收到此错误消息。我很困惑。这个/register/index 是怎么来的?

我需要在哪里更改错误消息?

提前致谢。

【问题讨论】:

  • 您在哪里看到该消息?您可以添加您的 GSP 或控制器代码的相关位吗?

标签: grails spring-security grails-plugin


【解决方案1】:

问题在于您声明消息代码的方式,它不符合约定。看看Validation and Internationalization。在这里你可以找到正确的方法是
[Class Name].[Property Name].[Constraint Code]
所以你的约束应该是

user.username.unique=Username already exists. Please use other username.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2018-08-08
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多