Search code examples
javabean-validationmessage-bundle

JSF2: bean validation with custom message bundle


I know the issue is discussed in many posts in the forum, however I'm still having problems customizing the messages for the bean validation. Maybe some little things missing in configuration or file location...

When the validation call the message, the web page shows the message ID instead of the message text.

For example I see in the web page {messages.myMessageId}

I have:

src/com/myapp/view/validationvalidation_en.poperties

webuser_name_notnull = Name is required

faces-config.xml

<message-bundle>
        com.myapp.view.validation
</message-bundle>

webuser.java

@NotNull(message = "{webuser_name_notnull}")
private String name;

Many thanks for support!


Solution

  • As per chapter 4.3.1.1 of the JSR303 specification, the custom JSR303 validation messages file needs to have exactly the name ValidationMessages.properties and it needs to be placed in the root of the classpath, in your case thus directly in the src folder.

    Please note that Bean Validation is not part of JSF. It's part of Java EE of which JSF is also a part. The JSF <message-bundle> only definies the location of JSF default validation messages (such as the one which you see on required="true"), not the JSR303 Bean Validation messages.