必填字段的自定义JSF验证器

实现EditableValueHolder接口的JSF组件具有两个属性“ required”和“ requiredMessage” –一个标志,指示用户需要输入/选择非空值,以及一个用于验证消息的文本。 我们可以使用它,但是它不够灵活,我们不能直接在视图中(facelets或jsp)对消息进行参数化,我们必须做一些适当的消息定制。 附加到任何必填字段的自定义验证器如何处理? 我们将写一个。 首先,我们需要在标签库中注册这样的验证器。
<?xml version='1.0'?>
<facelet-taglib version='2.0' ... >
    <namespace>http://ip.client/ip-jsftoolkit/validator</namespace>
    <tag>
        <tag-name>requiredFieldValidator</tag-name>
        <validator>
            <validator-id>ip.client.jsftoolkit.RequiredFieldValidator</validator-id>
        </validator>
        <attribute>
            <description>Resource bundle name for the required message</description>
            <name>bundle</name>
            <required>false</required>
            <type>java.lang.String</type>
        </attribute>
        <attribute>
            <description>Key of the required message in the resource bundle</description>
            <name>key</name>
            <required>false</required>
            <type>java.lang.String</type>
        </attribute>
        <attribute>
            <description>Label string for the required message</description>
            <name>label</name>
            <required>false</required>
            <type>java.lang.String</type>
        </attribute>
    </tag>
</facelet-taglib>

为了获得高度的灵活性,我们定义了三个属性。 一个简单的用法是

<h:outputLabel for='myInput' value='#{text['myinput']}'/>
<h:inputText id='myInput' value='...'>
    <jtv:requiredFieldValidator label='#{text['myinput']}'/>
</h:inputText>

验证器类本身并不困难。 根据'key'参数(所需消息的密钥)和'label'参数(对应标签的文本),有四种情况下如何获取消息。

/**
 * Validator for required fields.
 */
@FacesValidator(value = RequiredFieldValidator.VALIDATOR_ID)
public class RequiredFieldValidator implements Validator
{
    /** validator id */
    public static final String VALIDATOR_ID = 'ip.client.jsftoolkit.RequiredFieldValidator';

    /** default bundle name */
    public static final String DEFAULT_BUNDLE_NAME = 'ip.client.jsftoolkit.validator.message';

    private String bundle;
    private String key;
    private String label;

    @Override
    public void validate(FacesContext facesContext,
        UIComponent component, Object value) throws ValidatorException
    {
        if (!UIInput.isEmpty(value)) {
           return;
        }

        String message;
        String bundleName;

        if (bundle == null) {
           bundleName = DEFAULT_BUNDLE_NAME;
        } else {
           bundleName = bundle;
        }

        if (key == null && label == null) {
            message = MessageUtils.getMessageText(
               MessageUtils.getResourceBundle(facesContext, bundleName),
                  'jsftoolkit.validator.emptyMandatoryField.1');
        } else if (key == null && label != null) {
            message = MessageUtils.getMessageText(
               MessageUtils.getResourceBundle(facesContext, bundleName),
                  'jsftoolkit.validator.emptyMandatoryField.2', label);
        } else if (key != null && label == null) {
            message = MessageUtils.getMessageText(
               MessageUtils.getResourceBundle(facesContext, bundleName), key);
        } else {
            message = MessageUtils.getMessageText(
               MessageUtils.getResourceBundle(facesContext, bundleName), key, label);
        }

        throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, StringUtils.EMPTY));
       
        // getter / setter
        ... 
    }
}

MessageUtils是一个实用程序类,用于获取ResourceBundle和消息文本。 我们在资源包(属性文件)中还需要两个文本

jsftoolkit.validator.emptyMandatoryField.1=Some required field is not filled in.
jsftoolkit.validator.emptyMandatoryField.2=The required field '{0}' is not filled in.

以及web.xml中的以下上下文参数

<context-param>
    <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
    <param-value>true</param-value>
</context-param>

这种解决方案并不理想,因为我们需要两次定义标签文本(如#{text ['myinput']}),并将验证器附加到要验证的每个字段。 下一篇文章中将提供一个针对多个字段的更好的通用验证器。 敬请关注!

参考:来自JCG合作伙伴 Oleg Varaksin的必填字段的自定义JSF验证器,位于“ 软件开发思想”博客上。


翻译自: https://www.javacodegeeks.com/2012/05/custom-jsf-validator-for-required.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值