如何从日历验证日期 [英] How to validate a date from a Calendar

查看:125
本文介绍了如何从日历验证日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have tried the following code (I have followed an example) to test a date choosen from a calendar. If the date don't exist in my database, then a validation error message should be shown to the enduser. However, the error does not end up in the <h:message>, instead it is being logged to the server log.

查看:

<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捕获并处理它,以便它可以相应地在消息组件中显示它.

Look at your code once again, you're catching and printing the ValidatorException inside the validator yourself! You should not catch and log the ValidatorException yourself. You should let it go and let JSF catch and handle it so that it can display it in the message component accordingly.

摆脱验证器中的try块,并将throws子句添加到validate()方法中.

Get rid of the try block in the validator and add the throws clause to the validate() method.

@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"));
    } 
}   

这篇关于如何从日历验证日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆