I'm new to JSF development and do already have some troubles with the bean validation I can't get to worK:
As a Servlet container, I am using Tomcat 8.0.22 together with JSF 2.2 (Mojarra 2.2).
The problem is that the validation annotations in the code aren't being triggered.
For example, I've got a class customer with an attribute name, which is supposed to be filled by an <h:inputText>
.
The input is passed problemless to the entity, but the annotated validations aren't triggered.
Neither @NotNull
nor @Size
or anything else is triggered, so I guess it's a problem with Tomcat rather than JSF.
I've got the following Jars:
- bval-core-0.5.jar
- bval-jsr303-0.5.jar
- validation-api-1.0.0.GA.jar
in the WEB-INF/lib Folder of my WebApp and in the lib folder of Tomcat. Of course they're in the apps classpath as well.
I already tried it with the hibernate validator as well but can't get it running either.
I don't know what I'm not getting here and am thankful for any help!
Thanks in advance!
Benedikt
Here the code:
User.java:
package com.example;
import java.io.Serializable;
import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
@Size(min = 1, message = "Please enter username")
private String username;
@NotNull(message = "Please enter password")
private String password;
@NotNull(message = "Please enter email")
private String email;
private Date birthdate;
...
}
Register.java:
package com.example;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
@ManagedBean
@ViewScoped
public class Register implements Serializable {
private static final long serialVersionUID = 1L;
private User user;
@PostConstruct
public void init() {
user = new User();
}
public void submit() {
FacesMessage message = new FacesMessage("Registration succesful for: "
+ user.getUsername() + ", Username is null: " + (user.getUsername() == null));
FacesContext.getCurrentInstance().addMessage(null, message);
}
public User getUser() {
return user;
}
}
register.xhtml:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Insert title here</title>
</h:head>
<h:body>
<h:form id="Form">
<h:panelGrid columns="3">
<h:outputLabel for="username">Username</h:outputLabel>
<h:inputText id="username" value="#{register.user.username}">
<f:ajax event="blur" render="usernameMessage" />
</h:inputText>
<h:message id="usernameMessage" for="username" />
<h:outputLabel for="password">Password</h:outputLabel>
<h:inputSecret id="password" value="#{register.user.password}" redisplay="true">
<f:ajax event="blur" render="passwordMessage" />
</h:inputSecret>
<h:message id="passwordMessage" for="password" />
<h:outputLabel for="email">Email</h:outputLabel>
<h:inputText id="email" value="#{register.user.email}">
<f:ajax event="blur" render="emailMessage" />
</h:inputText>
<h:message id="emailMessage" for="email" />
<h:outputLabel for="birthdate">Birthdate (yyyy-MM-dd)</h:outputLabel>
<h:inputText id="birthdate" value="#{register.user.birthdate}">
<f:convertDateTime pattern="yyyy-MM-dd" />
<f:ajax event="blur" render="birthdateMessage" />
</h:inputText>
<h:message id="birthdateMessage" for="birthdate" />
<h:panelGroup />
<h:commandButton value="Register" action="#{register.submit}">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
<h:messages globalOnly="true" layout="table" />
</h:panelGrid>
</h:form>
</h:body>
</html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>JSFFaceletsTutorial</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>