我可以使用JSF标记中指定的选项制作JSF验证程序吗? [英] Can I make a JSF Validator with options specified in the JSF markup?

查看:59
本文介绍了我可以使用JSF标记中指定的选项制作JSF验证程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:我有一个JSF验证程序,因此可以验证电子邮件:

For example: I have a JSF Validator to validate e-mail thusly:

@FacesValidator(value="validateEmail")
public class Email implements Validator
{

private static final String EMAIL_REGEXP =
        "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

@Override
public void validate(FacesContext context, UIComponent c, Object val) throws ValidatorException
{
    String email = (String) val;
    Pattern mask = null;
    mask = Pattern.compile(EMAIL_REGEXP);
    Matcher matcher = mask.matcher(email);

    if (!matcher.matches()) {
        FacesMessage message = new FacesMessage();
        message.setDetail("Must be of the form xxx@yyy.zzz");
        message.setSummary("E-mail Addresss not valid");
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        throw new ValidatorException(message);
    }
}
}

如果用户不输入电子邮件,此验证器将引发异常.但是有时我想将电子邮件设置为可选字段.有没有一种方法可以不需要我编写其他验证器?

This validator will throw an exception if the user doesn't enter an e-mail. However sometimes I want to make the e-mail an optional field. Is there a way to do this that doesn't require me to write a different validator?

理想情况下,我希望它在使用它的JSF标记中的某个地方检查参数.

Ideally I would like it to check for a parameter somewhere in the JSF markup that uses it.

推荐答案

要回答您的具体问题,可以使用包含在同一组件中的<f:attribute>.

To answer your concrete question, you can make use of <f:attribute> which is enclosed in the same component.

<h:inputText>
    <f:validator validatorId="validateEmail" />
    <f:attribute name="foo" value="bar" />
</h:inputText>

validate()方法内部,第二个UIComponent参数的UIComponent#getAttributes()可用.

Inside the validate() method it's available by UIComponent#getAttributes() of the 2nd UIComponent argument.

String foo = component.getAttributes().get("foo"); // bar

但是,在您的特定情况下,更好的解决方案是在值为null或为空时让验证器return.

The better solution in your particular case, however, is just to let the validator return when the value is null or empty.

if (email == null || email.isEmpty()) {
    return;
}

在JSF 1.x中,这不是必需的,因为当输入为null或为空时,不会调用验证程序.但是在JSF 2.x中,此更改已更改为支持JSR303 bean验证.因此,如果要跳过对空字段的验证,则需要在每个JSF Validator中都进行检查.

In JSF 1.x this would not be necessary because validators wouldn't be called when the input is null or empty. But in JSF 2.x this has changed in order to support JSR303 bean validation. So if you want to skip validation on empty fields, you'd need to check this in every single JSF Validator.

请注意,当您在web.xml

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

然后您可以放心地从支票中省略email.isEmpty()部分,因为它始终是null而不是空字符串.

then you can safely omit the email.isEmpty() part from the check because it would always be null instead of an empty string.

无关与具体问题无关,您所拥有的模式已经过时.如今,允许使用Unicode字符在电子邮件地址中.

Unrelated to the concrete problem, the pattern which you've there is outdated. These days unicode characters are allowed in email addresses.

private static final String EMAIL_REGEXP = "([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)";

(请注意,不需要^$,因为Pattern已经隐式使用了它)

(note that ^ and $ are unnecessary because Pattern implicitly uses this already)

此外,我还建议直接在static final变量上执行Pattern#compile().编译模式是相对昂贵的,并且Pattern实例仍然是线程安全的.

Further I also suggest to do the Pattern#compile() directly on a static final variable. Compiling a pattern is relatively expensive and a Pattern instance is threadsafe anyway.

private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEXP);

这篇关于我可以使用JSF标记中指定的选项制作JSF验证程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆