具有facelets中属性的自定义验证器

时间:2010-08-05 19:13:03

标签: jsf facelets handler validation

我编写了一个自定义Validator,希望设置一个属性。我知道我必须编写一个Handler(TagHandler或ValidatorHandler?)来设置该属性,但我无法做到这一点。

<tag>
    <tag-name>validateCustom</tag-name>
    <validator>
        <validator-id>package.CUSTOM_VALIDATOR</validator-id>
    </validator>
</tag>

我在哪里放置handler-class元素?我如何编写处理程序,以便将属性传递给我的自定义验证器?

提前致谢!

2 个答案:

答案 0 :(得分:4)

以下是具有自定义属性骨架的验证器:

public class CustomValidator implements Validator, Serializable
{
    private static final long serialVersionUID = ...;
    private String someAttribute;

    public void setSomeAttribute(String someAttribute)
    {
        this.someAttribute = someAttribute;
    }

    @Override
    public void validate(FacesContext context, UIComponent component, Object value)
    throws ValidatorException 
    {
        // Do validation
    }
}

这是标记处理程序:

public class CustomValidatorHandler extends ValidateHandler
{
    private final TagAttribute someAttribute;

    public CustomValidatorHandler(ValidatorConfig config)
    {
        super(config);

        someAttribute = this.getAttribute("someAttribute");
    }

    @Override
    protected Validator createValidator(FaceletContext ctx)
    {
        CustomValidator result = (CustomValidator) ctx
        .getFacesContext()
        .getApplication()
        .createValidator("package.CUSTOM_VALIDATOR");

        if (someAttribute != null) result.setSomeAttribute(someAttribute.getValue(ctx));

        return result;
    }
}

这是taglib文件的一部分:

<tag>
    <tag-name>validateCustom</tag-name>
    <validator>
        <validator-id>package.CUSTOM_VALIDATOR</validator-id>
    <handler-class>package.CustomValidatorHandler</handler-class>
    </validator>
</tag>

答案 1 :(得分:0)

建议的解决方案不起作用,也过于复杂。 JSF 1.2中不需要处理程序类,并且根本不支持validator标记中的处理程序类。

所以你需要的只是。

验证者:

public class CustomValidator implements Validator, Serializable
{
    private static final long serialVersionUID = ...;
    private String someAttribute;

    public void setSomeAttribute(String someAttribute)
    {
        this.someAttribute = someAttribute;
    }

    @Override
    public void validate(FacesContext context, UIComponent component, Object value)
    throws ValidatorException 
    {
     // Do validation
    } 
}

在faces-config.xml中声明/注册自定义验证器

<validator>
    <validator-id>customValidator</validator-id>
    <validator-class>package.CustomValidator</validator-class>
</validator>

taglib.xml(例如my.taglib.xml):

<?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://myNS.com/jsf</namespace>
<tag>
    <tag-name>myCustomValidator</tag-name>
    <validator>
        <validator-id>customValidator</validator-id>
    </validator>
</tag>
</facelet-taglib>

最后在web.xml中注册taglib.xml:

<context-param>
    <param-name>facelets.LIBRARIES</param-name>
    <param-value>/WEB-INF/my.taglib.xml</param-value>
</context-param>

所以这一切都不需要处理程序: - )