I'm creating a JSF 2-application and I'm trying to use form validation in the bean instead of the faces-page. I'd also like to use a .properties file to store the messages.
I looked at this question but I don't think I have the properties-file set up correctly.
Let's say I have a bean called User in a package called 'dev':
@ManagedBean
@SessionScoped
public class User implements Serializable {
@Pattern(pattern=".+@.+\\.[a-z]+", message="{dev.User.emailAddress}")
private String emailAddress;
// getter/setter
}
I've also created a file 'ValidationMessages.properties' in WEB-INF/classes (I'm using Netbeans 7.0.1)
In the ValidationMessages.properties file I've got this key/value-line:
dev.User.emailAddress=Custom invalid email message
And my view (user.xhtml) looks like this:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>User registration</title>
</h:head>
<h:body>
<h:form>
<h:inputText id="emailAddress" value="#{user.emailAddress}" required="true" />
<h:message for="emailAddress" />
<h:commandButton value="Ok" action="null" />
</h:form>
</h:body>
When I enter an invalid email and press the button, the validation message in the web page reads:
{dev.User.emailAddress}
instead of
Custom invalid email message
Could the problem be that I haven't registered my properties file in web.xml?
I should switch required="true" to @NotNull in the bean too. Is there anything more I need to do than insert this:
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
as explained in BalusC's blog?
Thanks in advance!
/ Dennis