如何从日历验证日期

时间:2011-07-27 15:22:02

标签: java validation jsf-2 primefaces

我尝试了以下代码(我已经按照一个示例)来测试从日历中选择的日期。如果我的数据库中不存在该日期,则应向最终用户显示验证错误消息。但是,错误不会在<h:message>中结束,而是记录到服务器日志中。

查看:

<p:calendar id="date1" value="#{bean.date1}" showOn="button"> 
    <f:validator validatorId="calendarValidator" />
    <f:ajax execute="date1" render="calendarmessage" />
</p:calendar>
<h:message id="calendarmessage" for="date1" />

验证

@FacesValidator("calendarValidator")
public class CalendarValidator implements Validator{

    @Override  
    public void validate(FacesContext context, UIComponent component, Object value) {
        java.util.Date date2 = (java.util.Date) value;

        try {
            if (validateDate(date2)) {
                throw new ValidatorException(new FacesMessage("A valid date"));
            } else {
                throw new ValidatorException(new FacesMessage("date dont figure in the database"));
            } 
        } catch(Exception e) {
            e.printStackTrace();
        }   
    }
}

服务器日志:

INFO: date invalide
GRAVE: javax.faces.validator.ValidatorException: date dont figure in the database
at DAOKPI.CalendarValidator.validate(CalendarValidator.java:60)
at javax.faces.component.UIInput.validateValue(UIInput.java:1149)
at javax.faces.component.UIInput.validate(UIInput.java:967)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
at javax.faces.component.UIInput.processValidators(UIInput.java:698)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1

2 个答案:

答案 0 :(得分:4)

再次查看您的代码,您正在捕捉并自己在验证器内打印ValidatorException!你不应该自己捕获并记录ValidatorException。您应该放手让JSF捕获并处理它,以便它可以相应地在消息组件中显示它。

删除验证程序中try块的,并将throws子句添加到validate()方法。

@Override  
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    java.util.Date date2 = (java.util.Date) value;

    if (validateDate(date2)) {
        throw new ValidatorException(new FacesMessage("A valid date")); // Btw: I wonder how that makes sense... That's actually not a validation error.
    } else {
        throw new ValidatorException(new FacesMessage("date dont figure in the database"));
    } 
}   

答案 1 :(得分:0)

除了@Balusc已经在his answer中提到的验证器中的问题之外,你应该使用PrimeFaces组件:

<p:calendar id="date1" value="#{bean.date1}" showOn="button"> 
    <f:validator validatorId="calendarValidator" />
    <p:ajax execute="date1" update="calendarmessage" />
</p:calendar>
<p:message id="calendarmessage" for="date1" />