简体   繁体   English

如何使用CDI创建验证器

[英]How to create Validator with CDI

I want to implement custom validator in which I can use CDI and datasource. 我想实现可在其中使用CDI和数据源的自定义验证器。 I tested this code: 我测试了这段代码:

<h:panelGroup>Session ID</h:panelGroup>
<h:panelGroup>
    <h:inputText id="sessionid" value="#{DatabaseController.formMap['sessionid']}" >
        <f:validateLength minimum="0" maximum="15"/>
        <f:validator validatorId="ValidatorController" >
        </f:validator>
        <f:ajax event="blur" render="sessionidMessage" />                                          
    </h:inputText>
    <h:message id="sessionidMessage" for="sessionid" />
</h:panelGroup>

This is the validator: 这是验证器:

    @FacesValidator("ValidatorController")

    public class FormValidator implements Validator {

        public FormValidator() {
        }

        @Override
        public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
            if (value.equals("test")) {
                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        "  Session ID is already in use, please choose another.", null));
            }
        }
}

This code works fine. 此代码可以正常工作。 I also tried to implement this code in order to use CDI into the validator: 我还尝试实现此代码,以便在验证器中使用CDI:

<h:panelGroup>Session ID</h:panelGroup>
<h:panelGroup>
    <h:inputText id="sessionid" value="#{DatabaseController.formMap['sessionid']}" >
        <f:validateLength minimum="0" maximum="15"/>
        <f:validator binding="#{ValidatorController}" >
            <f:attribute name="type" value="sessionid" />
        </f:validator>
        <f:ajax event="blur" render="sessionidMessage" />                                          
    </h:inputText>
    <h:message id="sessionidMessage" for="sessionid" />
</h:panelGroup>

This is the validator: 这是验证器:

@Named("ValidatorController")

public class FormValidator implements Validator {

    public FormValidator() {
    }

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        if (value.equals("test")) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "  Session ID is already in use, please choose another.", null));
        }
    }

}

For some reason the second example is not working. 由于某些原因,第二个示例不起作用。

You should be more specific, what fails? 您应该更具体些,什么失败? It does not compile? 它不编译吗? It does not run? 它不运行? What is the exception, etc. And also it is not clear what are you trying to achieve in 2nd case. 有什么例外,等等。也不清楚您在第二种情况下要达到什么目标。

In general, you can not pass the attribute to the f:validator by nesting f:attribute insside. 通常,不能通过嵌套f:attribute insside将属性传递给f:validator。 Make your code look like this: 使您的代码如下所示:

<f:validator binding="#{ValidatorController}" (session id in your case) />
<f:attribute name="type" value="sessionid" />

Later on you can search for the component parameter inside component parameter Map: 以后你可以搜索元件参数地图里面的元件参数:

context.getExternalContext().getRequestParameterMap();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM