2012-08-07 70 views
0

我有一个域类:如何呈现自定义错误消息?

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 
    } 
} 

这是我用弹簧安全的类。在我/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来自?

我需要更改错误信息的位置?

在此先感谢。

+0

你在哪里看到该消息?你可以添加你的GSP或控制器代码的相关位吗? – 2012-08-07 08:14:52

回答

2

问题是你已经声明了消息代码的方式,它并没有遵循惯例。看看Validation and Internationalization。在这里你可以找到正确的方法是
[Class Name].[Property Name].[Constraint Code]
所以,你的约束应该是

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

'user.username.unique',camel-case。 – 2012-08-07 09:12:28