如何验证两个组件的值是否相同?JSF

TCM*_*TCM 3 jsf

由于我们在Asp.Net中有comparevalidator,我们在JSF中有什么来验证两个字段的值是否相同?我想验证密码和confirmPassword字段.

Bal*_*usC 7

不,基本的JSF实现中不存在这样的验证器.您基本上需要在组的最后一个组件上运行验证器,并使用您要验证的其他组件UIViewRoot#findComponent().例如

public void validate(FacesContext context, UIComponent component, Object value) {
    UIComponent otherComponent = context.getViewRoot().findComponent("otherClientId");
    Object otherValue = ((UIInput) otherComponent).getValue();
    // ...
}
Run Code Online (Sandbox Code Playgroud)

另请参阅此文章以获取更多背景信息和具体示例.

另一方面,如果您已经使用JSF2,那么您也可以使用ajaxical验证:

<h:form>
    <f:event type="postValidate" listener="#{bean.validate}" />
     ...
</h:form>
Run Code Online (Sandbox Code Playgroud)

..where #{bean.validate}方法看起来像这样:

public void validate(ComponentSystemEvent e) {
    UIComponent form = e.getComponent();
    UIComponent oneComponent = form.findComponent("oneClientId");
    UIComponent otherComponent = form.findComponent("otherClientId");
    // ...
} 
Run Code Online (Sandbox Code Playgroud)

另请参阅本文以获取更多JSF2验证示例.