【问题标题】:How do i validate two component's value are same or not? JSF如何验证两个组件的值是否相同? JSF
【发布时间】:2011-01-28 05:09:55
【问题描述】:

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

【问题讨论】:

    标签: jsf


    【解决方案1】:

    不,这样的验证器在基本的 JSF 实现中不存在。您基本上需要在组的 last 组件上运行验证器,并使用 UIViewRoot#findComponent() 获取您想要验证的 other 组件。例如

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

    有关更多背景信息和具体示例,另请参阅this article

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

    <h:form>
        <f:event type="postValidate" listener="#{bean.validate}" />
         ...
    </h:form>
    

    ..where #{bean.validate} 方法如下所示:

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

    有关更多 JSF2 验证示例,另请参阅 this article

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-19
      • 2018-03-02
      • 2013-09-21
      • 2020-09-10
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多