编程语言
首页 > 编程语言> > java-如何从日历验证日期

java-如何从日历验证日期

作者:互联网

我尝试了以下代码(我遵循了一个示例)来测试从日历中选择的日期.如果我的数据库中不存在该日期,则应向最终用户显示一条验证错误消息.但是,错误不会最终出现在< 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

解决方法:

再次查看您的代码,您将自己在验证器中捕获并打印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"));
    } 
}   

标签:validation,primefaces,jsf-2,java
来源: https://codeday.me/bug/20191102/1990919.html