c12*_*c12 5 validation jsf jsf-2
你如何比较两个字符串在JSF验证器中的相等性?
if (!settingsBean.getNewPassword().equals(settingsBean.getConfirmPassword())) {
save = false;
FacesUtils.addErrorMessage(null, "Password and Confirm Password are not same", null);
}
Run Code Online (Sandbox Code Playgroud)
Bal*_*usC 17
使用法线Validator并将第一个组件的值作为第二个组件的属性传递.
<h:inputSecret id="password" binding="#{passwordComponent}" value="#{bean.password}" required="true"
requiredMessage="Please enter password" validatorMessage="Please enter at least 8 characters">
<f:validateLength minimum="8" />
</h:inputSecret>
<h:message for="password" />
<h:inputSecret id="confirmPassword" required="#{not empty passwordComponent.value}"
requiredMessage="Please confirm password" validatorMessage="Passwords are not equal">
<f:validator validatorId="equalsValidator" />
<f:attribute name="otherValue" value="#{passwordComponent.value}" />
</h:inputSecret>
<h:message for="confirmPassword" />
Run Code Online (Sandbox Code Playgroud)
(请注意,binding上面的示例是原样;您不应该将它绑定到bean属性!)
同
@FacesValidator(value="equalsValidator")
public class EqualsValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
Object otherValue = component.getAttributes().get("otherValue");
if (value == null || otherValue == null) {
return; // Let required="true" handle.
}
if (!value.equals(otherValue)) {
throw new ValidatorException(new FacesMessage("Values are not equal."));
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果您碰巧使用JSF实用程序库OmniFaces,那么您可以使用<o:validateEquals>它."确认密码"的确切情况在<o:validateEqual>展示会上展示.
| 归档时间: |
|
| 查看次数: |
8311 次 |
| 最近记录: |