Prezados
Estou sofrendo com um pequeno exemplo que diz que valida, mas até agora só consegui alterar as mensagens padrão de erro. Segue abaixo o código-fonte:
Classe SimpleLogin.java
package roseindia;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
public class SimpleLogin{
String loginname;
String password;
public SimpleLogin(){}
public String getLoginname(){
return loginname;
}
public void setLoginname(String loginname){
this.loginname = loginname;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
public String CheckValidUser(){
if(loginname.equals("chandan") && password.equals("chand")){
System.out.println("chandan");
return "success";
}
else{
return "fail";
}
}
public void validate(FacesContext arg0, UIComponent arg1, Object arg2)
throws ValidatorException {
// TODO Auto-generated method stub
if (arg2.equals("")) {
FacesMessage message = new FacesMessage();
message.setDetail("login em branco");
message.setSummary("login em branco");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
//FacesContext.getCurrentInstance().addMessage(arg1.getClientId(arg0), message);
}
}
}
index.jsp
<%@ page contentType="text/html"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<f:view>
<html>
<head><title>JSF Simple Login Example</title></head>
<body>
<h:form>
<table>
<tr>
<td><h:outputText value="Enter Login ID: " /></td>
<td>
<h:inputText id="loginname" value="#{SimpleLogin.loginname}" required="true" validator="#{SimpleLogin.validate}" />
<font color="#FF0000"><h:message for="loginname"/></font>
</td>
</tr>
<tr>
<td><h:outputText value="Enter Password:" /></td>
<td>
<h:inputSecret id="password" value="#{SimpleLogin.password}" required="true" />
<font color="#FF0000"><h:message for="password"/></font>
</td>
</tr>
<tr>
<td> </td>
<td><h:commandButton value="Login" action="#{SimpleLogin.CheckValidUser}" /></td>
</tr>
</table>
</h:form>
</body>
</html>
</f:view>
faces-config.xml
<?xml version="1.0"?>
<!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>
<managed-bean>
<managed-bean-name>SimpleLogin</managed-bean-name>
<managed-bean-class>roseindia.SimpleLogin</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/login.jsp</from-view-id>
<navigation-case>
<from-action>#{SimpleLogin.CheckValidUser}</from-action>
<from-outcome>success</from-outcome>
<to-view-id>resultforsuccess.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{SimpleLogin.CheckValidUser}</from-action>
<from-outcome>fail</from-outcome>
<to-view-id>resultforfail.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<validator>
<validator-id>roseindia.valida</validator-id>
<validator-class>roseindia.Validacao</validator-class>
</validator>
<application>
<message-bundle>roseindia.MyMessages</message-bundle>
</application>
</faces-config>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>validacao</display-name>
<!-- servlet -->
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- servlet-mapping -->
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>javax.faces.CONFIG_FILE</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
</web-app>
Validacao.java
package roseindia;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
public class Validacao implements Validator {
//@Override
public void validate(FacesContext arg0, UIComponent arg1, Object arg2)
throws ValidatorException {
// TODO Auto-generated method stub
if (arg2.equals("")) {
FacesMessage message = new FacesMessage();
message.setDetail("login em branco");
message.setSummary("login em branco");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
//FacesContext.getCurrentInstance().addMessage(arg1.getClientId(arg0), message);
}
}
}
Alguém sabe como fazer isso funcionar?