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一样:

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

解决方法

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

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

这是facelet文件

  1. <h:outputLabel value="Enter your email address" rendered="#{!cc.attrs.registration.subRegistration}" />
  2. <h:inputText label="Email" id="textEmail1" value="#{cc.attrs.registration.email}" rendered="#{!cc.attrs.registration.subRegistration}" required="true" maxlength="128" size="35"></h:inputText>
  3. <h:message for="textEmail1" rendered="#{!cc.attrs.registration.subRegistration}"></h:message>
  4.  
  5. <h:outputLabel value="Re-enter your email address confirmation:" rendered="#{!cc.attrs.registration.subRegistration and cc.attrs.duplicateEmailrequired}" />
  6. <h:inputText label="Email repeat" id="textEmail2" rendered="#{!cc.attrs.registration.subRegistration and cc.attrs.duplicateEmailrequired}" maxlength="64" size="35">
  7. <f:validator validatorId="duplicateFieldValidator" />
  8. <f:attribute name="field1Id" value="#{component.parent.parent.clientId}:textEmail1" />
  9. </h:inputText>
  10. <h:message for="textEmail2" rendered="#{!cc.attrs.registration.subRegistration and cc.attrs.duplicateEmailrequired}"></h:message>

这是验证器类:

  1. package ca.triumf.mis.trevents.jsf.validator;
  2.  
  3. import javax.faces.application.FacesMessage;
  4. import javax.faces.component.UIComponent;
  5. import javax.faces.component.UIInput;
  6. import javax.faces.context.FacesContext;
  7. import javax.faces.validator.FacesValidator;
  8. import javax.faces.validator.Validator;
  9. import javax.faces.validator.ValidatorException;
  10.  
  11. @FacesValidator(value="duplicateFieldValidator")
  12. public class DuplicateFieldValidator implements Validator {
  13.  
  14. @Override
  15. public void validate(FacesContext context,UIComponent component,Object value)
  16. throws ValidatorException {
  17. // Obtain the client ID of the first field from f:attribute.
  18. System.out.println(component.getFamily());
  19. String field1Id = (String) component.getAttributes().get("field1Id");
  20.  
  21. // Find the actual JSF component for the client ID.
  22. UIInput textInput = (UIInput) context.getViewRoot().findComponent(field1Id);
  23. if (textInput == null)
  24. throw new IllegalArgumentException(String.format("Unable to find component with id %s",field1Id));
  25. // Get its value,the entered text of the first field.
  26. String field1 = (String) textInput.getValue();
  27.  
  28. // Cast the value of the entered text of the second field back to String.
  29. String confirm = (String) value;
  30.  
  31. // Check if the first text is actually entered and compare it with second text.
  32. if (field1 != null && field1.length() != 0 && !field1.equals(confirm)) {
  33. throw new ValidatorException(new FacesMessage("E-mail addresses are not equal."));
  34. }
  35. }
  36. }

猜你在找的Java相关文章