jsf多个验证器

时间:2010-10-21 07:43:47

标签: java validation jsf richfaces

我需要知道如何在多个字段中使用多个验证器,例如姓名和电子邮件ID,年龄和电话号码。我有一个线索,在ajax我可以使用多个validaors一个表单具有不同的字段,但在jsf只有一个字段如名称验证器或电子邮件验证器工作,如果我使用两个验证器为相同的表单,它不工作,这是我的代码

package com.dnb;

import java.util.Date;

import javax.faces.component.UIComponent;

public class UserBean 
{
 private String username;

 public String getUsername() 
 {
  return username;
 }

 public void setUserName(String UserName) {
  this.username = UserName;
 }
 private String emailid;

 public String getEmailId() 
 {
  return emailid;
 }

 public void setEmailId(String EmailId) {
  this.emailid = EmailId;
 }
 private int age;



 private Object city;

 public String getage() 
 {
  return "";
 }

 public void setAge(int Age) {
  this.age = age;
 }

 public String getcity() 
 {
  return "";
 }

 public void setcity(int City) {
  this.city = city;
 }
 public void success() {
        setProgressString(getProgressString() + "(Strored successfully)");
    }

    private void setProgressString(String string) {
  // TODO Auto-generated method stub

 }

 public String getProgressString() {
        return getProgressString();
    }
  private boolean buttonRendered = true;
     private boolean enabled=false;
     private Long startTime;



     public String startProcess() {
         setEnabled(true);
         setButtonRendered(false);
         setStartTime(new Date().getTime());
         return null;
     }

     public Long getCurrentValue(){
         if (isEnabled()){
             Long current = (new Date().getTime() - startTime)/1000;
             if (current>100){
                 setButtonRendered(true);
             }else if (current.equals(0)){
                 return new Long(1);
             }
             return (new Date().getTime() - startTime)/1000;
         } if (startTime == null) {
             return Long.valueOf(-1);
         }
         else
             return Long.valueOf(101);
     }

     public boolean isEnabled() {
         return enabled;
     }

     public void setEnabled(boolean enabled) {
         this.enabled = enabled;
     }

     public Long getStartTime() {
         return startTime;
     }

     public void setStartTime(Long startTime) {
         this.startTime = startTime;
     }

     public boolean isButtonRendered() {
         return buttonRendered;
     }

     public void setButtonRendered(boolean buttonRendered) {
         this.buttonRendered = buttonRendered;
     }


}

JSF:

<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>  
 <%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>  
 <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>  
 <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<html>

    <body>    
    <f:view>  
    <h:form id="ajaxValidatorForm">
        <rich:panel>
            <f:facet name="header">
                <h:outputText value="User Info:" />
            </f:facet>
            <h:panelGrid columns="4">


    <h:outputText value="Name:" />
                <h:inputText  value="#{userBean.username}" id="username" required="true" >   
                <f:validator validatorId="nameValidator" />                            
             <h:message for="username"></h:message>
                 </h:inputText>


                <h:outputText value="Email:" />
                <h:inputText  value="#{userBean.emailId}" id="emailid" required="true" >   
                <f:validator validatorId="nameValidator" />                            

    <h:message for="emailid"></h:message>

       </h:inputText>     



            </h:panelGrid>
        </rich:panel>
    </h:form>


   </f:view>  

    </body>  
   </html>  

名称验证员:

package com.dnb;

import javax.faces.application.FacesMessage; 


public class NameValidator implements Validator
{ 

 public void validate(FacesContext context, UIComponent component, Object value) 
    throws ValidatorException 
    { 

   String name = (String) value; 

   if(!name.contains("123")) 
   { 

   FacesMessage message = new FacesMessage(); 

   message.setSeverity(FacesMessage.SEVERITY_ERROR); 

   message.setSummary("Name must be 3 to 8 letters"); 

   message.setDetail("Name must be 3 to 8 letters"); 

   context.addMessage("userForm:Name", message); 

   throw new ValidatorException(message); 


 } 

 } 

电子邮件验证工具:

package com.dnb;

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 EmailValidator implements Validator
{ 

 public void validate(FacesContext context, UIComponent component, Object value) 
    throws ValidatorException 
    { 

   String email = (String) value; 

   if(!email.contains("@")) 
   { 

   FacesMessage message = new FacesMessage(); 

   message.setSeverity(FacesMessage.SEVERITY_ERROR); 

   message.setSummary("Email is not valid,Example@abc.com"); 

   message.setDetail("Email is not valid,Example@abc.com"); 

   context.addMessage("userForm:Email", message); 

   throw new ValidatorException(message); 


 } 

 } 


}

请为我的问题提供一些完美的答案。

如果我使用一个字段作为E.mail我是geeting验证者消息,如果我使用两个或更多字段,如姓名,电话号码我没有得到验证器消息。只有一个字段正在工作,如何在jsf验证器中使用多个字段

1 个答案:

答案 0 :(得分:1)

尝试在表单中写入<h:messages />标记。

如果ID存在问题,可能有助于查找错误,因为它会打印所有消息。