Olá galera,
Estou tentando reproduzir um código de uma video aula, e estou tendo uns resultados estranhos nos validadores JSF.
Para que alguém possa me ajudar, estarei colocando o código completo, o qual não é muito grande. E o erro está na imagem em anexo. Reparem as msgs de validação retornadas.
<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>ValidatorsJSF</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
<init-param>
<param-name>maxFileSize</param-name>
<param-value>20m</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<url-pattern>/faces/myFacesExtensionResource/*</url-pattern>
</filter-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<application>
<message-bundle>jvm.faces.Messages</message-bundle>
</application>
<validator>
<validator-id>javax.faces.Genre</validator-id>
<validator-class>jvm.faces.validator.GenreValidator</validator-class>
</validator>
</faces-config>
package jvm.faces.validator;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import org.apache.myfaces.shared_impl.util.MessageUtils;
@SuppressWarnings("unused")
public class GenreValidator implements Validator {
public static final String INVALID_MESSAGE_ID = "jvm.faces.validator.GenreValidator.INVALID";
public static final String VALIDATOR_ID = "javax.faces.Genre";
private boolean _transient = false;
public GenreValidator() {
}
@Override
public void validate(FacesContext facesContext, UIComponent uiComponent, Object value)
throws ValidatorException {
if(facesContext == null) throw new NullPointerException("facesContext");
if(uiComponent == null) throw new NullPointerException("uiComponent");
if(value == null){
return;
}
String genre = value instanceof String ? ((String)value) : value.toString();
if(! genre.equals("male") && ! genre.equals("female")){
Object[] args = {genre,uiComponent.getId()};
throw new ValidatorException(MessageUtils.getMessage(facesContext, INVALID_MESSAGE_ID, args));
}
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof GenreValidator)) return false;
return true;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<f:view>
<h:form>
<h:outputLabel for="inputRange" value="Please provide some info:"/>
<h:inputText id="inputRequired" required="true"/>
<h:message for="inputRequired"/>
<br/>
<br/>
<h:outputLabel for="inputLength" value="Please provide some info:"/>
<h:inputText id="inputLength" value="s">
<f:validateLength minimum="3" maximum="5"/>
</h:inputText>
<h:message for="inputLength"/>
<br/>
<br/>
<h:outputLabel for="inputRange" value="Please provide a valid number:"/>
<h:inputText id="inputRange" value="0">
<f:validateLongRange minimum="1" maximum="10"/>
</h:inputText>
<h:message for="inputRange"/>
<br/>
<br/>
<h:outputLabel for="inputMail" value="Please provide a valid email:"/>
<h:inputText id="inputMail" value="[email protected]">
<t:validateEmail/>
</h:inputText>
<h:message for="inputMail"/>
<br/>
<br/>
<h:outputLabel for="inputGenre" value="Please provide a valid genre:"/>
<h:inputText id="inputGenre" value="something">
<f:validator validatorId="javax.faces.Genre"/>
</h:inputText>
<h:message for="inputGenre"/>
<br/>
<br/>
<h:commandButton value="Validate!"/>
</h:form>
</f:view>
</body>
</html>
jvm.faces.validator.GenreValidator.INVALID = Validation Error
jvm.faces.validator.GenreValidator.INVALID_detail = Invalid genre: {0}.
