具有自定义组件属性的Validator

时间:2014-02-21 14:33:00

标签: jsf jsf-2

我想为自定义组件创建验证器,我希望传递一些属性。这就是代码的样子(它不是原始代码,但是以相同的方式实现):

自定义组件(customComponent.xhtml)

<h:body>
    <composite:interface componentType="compositeComponent">
        <composite:attribute name="name" required="true" />
        <composite:attribute name="value" required="true" />
        <composite:attribute name="values" required="true" />
        <composite:editableValueHolder name="validator" targets="#{cc.attrs.id}"/>
    </composite:interface>
    <composite:implementation>
        <h:selectOneMenu value="#{cc.attrs.value}" id="#{cc.attrs.id}">
            <f:selectItems value="#{cc.attrs.values}" var="item" itemValue="#{item.value}" itemLabel="#{item.label}" />
            <composite:insertChildren/>
        </h:selectOneMenu>
    </composite:implementation>
</h:body>

如您所见,我想将验证器传递给h:selectOneMenu。组件可以(更确切地说'应该'因为它当前不起作用)以这种方式使用:

<ns:customComponent name="myComp" value="#{controller.value}" values="#{controller.values}">
    <f:validator validatorId="myValidator" for="validator">
        <f:attribute name="param1" value="param1Value"/>
        <f:attribute name="param1" value="param1Value"/>
    </validator>
</ns:customComponent>

我测试了这段代码,如果我没有将属性传递给它,则调用验证器。

<ns:customComponent name="myComp" value="#{controller.value}" values="#{controller.values}">
    <f:validator validatorId="myValidator" for="validator"/>
</ns:customComponent>

我发现属性可以通过这种方式传递:

<ns:customComponent name="myComp" value="#{controller.value}" values="#{controller.values}">
    <f:validator validatorId="myValidator" for="validator"/>
    <f:attribute name="param1" value="param1Value"/>
    <f:attribute name="param1" value="param1Value"/>
</ns:customComponent>

但是(据我所知)只会向自定义组件注入验证器(这就是验证器上设置for="validator"的原因)所以我将无法获得这些属性。如何将属性传递给此验证器?

顺便说一句。如果有可能我会想要将参数作为嵌套元素传递,因为它看起来更清晰。这一个:

<f:selectOneMenu>
    <f:validator validatorId="myValidator">
        <f:attribute name="param1" value="value1"/>
    </f:validator>
</f:selectOneMenu>

而不是这一个:

<f:selectOneMenu>
    <f:validator validatorId="myValidator"/>
    <f:attribute name="param1" value="value1"/>
</f:selectOneMenu>

1 个答案:

答案 0 :(得分:4)

我发现<f:validator/>无法嵌套元素,因此这个元素不起作用:

<f:validator validatorId="myValidator">
    <f:attribute name="param1" value="value1"/>
</f:validator>

为了解决我的问题,我创建了自定义验证器。要做到这一点,我必须:

taglib.xml目录中创建WEB-INF文件。

<?xml version="1.0"?>
<!DOCTYPE facelet-taglib PUBLIC
  "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
  "http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib>
    <namespace>http://customtag.com/tags</namespace>
    <tag>
        <tag-name>uniqueValidator</tag-name>
        <validator>
            <validator-id>simpleValidator</validator-id>
        </validator>
        <!-- To show hints on this component add this but it's not required -->
        <attribute>
            <description>List of elements to check. Validation succeeds if each item is unique (equals() method is used to compare items).</description>
            <name>items</name>
            <required>true</required>
        </attribute>
    </tag>
</facelet-taglib>

taglib.xml

中注册web.xml
<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/taglib.xml</param-value>
</context-param>

编写验证码

package validator;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

@FacesValidator("simpleValidator")
public class SimpleValidator implements Validator {

    private List<Object> items;

    @Override
    public void validate(final FacesContext arg0, final UIComponent arg1, final Object arg2) throws ValidatorException {
        // use items list
    }

    public void setItems(final List<Object> items) {
        this.items = items;
    }

}

这是它在视图/复合组件中的使用方式:

<mycomp:custom name="test11">
    <myval:uniqueValidator items="#{model.values}" for="validator"/>
</mycomp:custom>

当然要在自定义组件中使用验证器,我必须定义editableValueHolder并使用insertChildren注入/粘贴它(请参阅我的问题)。