JSF 2.0:验证2个InputSecret字段的相等性(确认密码)而不编写代码?

前端之家收集整理的这篇文章主要介绍了JSF 2.0:验证2个InputSecret字段的相等性(确认密码)而不编写代码?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用JSF 2.0和Glassfish开发纯 JavaEE6应用程序.
我的JSF实现是Primefaces(除了Glassfish提供的Mojarra).

我想验证JSF表单中2个密码字段的值是否相等.
对于Seam,有一个整洁的组件< s:validateEquality for =“pw1”/>.
我想在没有Seam的情况下做同样的事情,只需使用JSF(或者可能是JSF库的一个组件).到目前为止,我只看到了使用自定义验证器验证表单的示例.但我想比较这些字段而不编写Java代码Javascript代码.
那可能吗?

这与Seam一样:

...
<h:inputSecret id="passwort" value="#{personHome.instance.password}" 
    redisplay="true" required="true">
  <f:validateLength minimum="8"/>
  <a:support event="onblur" reRender="passwortField" bypassUpdates="true" ajaxSingle="true" />
</h:inputSecret>
...    
<h:inputSecret id="passwort2" required="true" redisplay="true">
  <!-- find the JSF2.0-equivalent to this tag: -->
  <s:validateEquality for="passwort"/>
  <a:support event="onblur" reRender="passwort2Field" bypassUpdates="true" ajaxSingle="true" />
</h:inputSecret>
...

解决方法

Seam3 Faces module将在即将发布的Alpha3版本中支持“跨领域表单验证”.对于最小代码解决方案,这是您最好的选择,请参阅此 blog for howto.

或者,我通过使用f:attribute标记将另一个表单字段的clientId传递给自定义验证器,然后使用传递给自定义验证器的UIComponent通过id访问另一个字段来以编程方式完成此操作.

这是facelet文件

<h:outputLabel value="Enter your email address" rendered="#{!cc.attrs.registration.subRegistration}" />
<h:inputText label="Email" id="textEmail1" value="#{cc.attrs.registration.email}" rendered="#{!cc.attrs.registration.subRegistration}" required="true" maxlength="128" size="35"></h:inputText>
<h:message for="textEmail1" rendered="#{!cc.attrs.registration.subRegistration}"></h:message>

<h:outputLabel value="Re-enter your email address confirmation:" rendered="#{!cc.attrs.registration.subRegistration and cc.attrs.duplicateEmailrequired}" />
<h:inputText label="Email repeat" id="textEmail2" rendered="#{!cc.attrs.registration.subRegistration and cc.attrs.duplicateEmailrequired}" maxlength="64" size="35">
    <f:validator validatorId="duplicateFieldValidator" />
    <f:attribute name="field1Id" value="#{component.parent.parent.clientId}:textEmail1" />
</h:inputText>
<h:message for="textEmail2" rendered="#{!cc.attrs.registration.subRegistration and cc.attrs.duplicateEmailrequired}"></h:message>

这是验证器类:

package ca.triumf.mis.trevents.jsf.validator;

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

@FacesValidator(value="duplicateFieldValidator")
public class DuplicateFieldValidator implements Validator {

@Override
public void validate(FacesContext context,UIComponent component,Object value)
        throws ValidatorException {
    // Obtain the client ID of the first field from f:attribute.
    System.out.println(component.getFamily());
    String field1Id = (String) component.getAttributes().get("field1Id");

    // Find the actual JSF component for the client ID.
    UIInput textInput = (UIInput) context.getViewRoot().findComponent(field1Id);
    if (textInput == null)
        throw new IllegalArgumentException(String.format("Unable to find component with id %s",field1Id));
    // Get its value,the entered text of the first field.
    String field1 = (String) textInput.getValue();

    // Cast the value of the entered text of the second field back to String.
    String confirm = (String) value;

    // Check if the first text is actually entered and compare it with second text.
    if (field1 != null && field1.length() != 0 && !field1.equals(confirm)) {
        throw new ValidatorException(new FacesMessage("E-mail addresses are not equal."));
    }
}
}

猜你在找的Java相关文章