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.
web.xml
[code]<?xml version="1.0" encoding="UTF-8"?>
ValidatorsJSF
index.jsp
Faces Servlet
javax.faces.webapp.FacesServlet
1
Faces Servlet
/faces/
.jsf
*.faces
<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>
[/code]
faces-config.xml
[code]<?xml version="1.0" encoding="UTF-8"?>
jvm.faces.Messages javax.faces.Genre jvm.faces.validator.GenreValidator [/code]GenreValidator.java
[code]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;
}
}[/code]
validators.jsp
[code]<%@ 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”%>
<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@.com">
<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>
[/code]Messages.properties
jvm.faces.validator.GenreValidator.INVALID = Validation Error
jvm.faces.validator.GenreValidator.INVALID_detail = Invalid genre: {0}.