java 如何验证日历中的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6846882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:34:19  来源:igfitidea点击:

How to validate a date from a Calendar

javavalidationjsf-2primefaces

提问by rym

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.

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

View:

看法:

<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" />

Validator:

验证器:

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

Server log:

服务器日志:

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

回答by BalusC

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

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

Get ridof the tryblock in the validator and add the throwsclause to the validate()method.

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

回答by Bj?rn Pollex

Aside from the problem in the validator that @Balusc already mentions in his answer, you should use PrimeFaces-components consitently:

除了@Balusc 在他的回答中已经提到的验证器中的问题之外,您应该始终使用 PrimeFaces-components:

<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" />