通过绑定获取的UIComponent#getValue()在另一个组件的验证器中不可用 [英] UIComponent#getValue() obtained via binding is not available in validator of another component

查看:142
本文介绍了通过绑定获取的UIComponent#getValue()在另一个组件的验证器中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚为什么将f:attribute标记的值附加到h:inputSecret标记时未传递.我对jsf相当陌生,但据我所知,属性可以附加到任何种类的组件上.这是代码:

I'm trying to figure out why an f:attribute tag's value isn't passed when attached to h:inputSecret tag. I'm quite new to jsf, but as far as I know attributes can be attached to any kind of component. Here is the code:

    <h:inputSecret id="passw" value="#{advertAdder.userPass}" 
                   required="true" validator="#{advertAdder.validatePasswords}">
        <f:attribute name="confirmedPass" value="#{advertAdder.passConfirmator.value}"/>
    </h:inputSecret>

    <h:inputSecret id="passwConfirm" required="true" 
                   binding="#{advertAdder.passConfirmator}"/>

以及想要访问此属性的方法:

and the method that wants to acces this attribute:

public void validatePasswords(FacesContext context, UIComponent component, Object value)
    {
        if (!value.equals(component.getAttributes().get("confirmedPass")))
        {
            FacesMessage mess = new FacesMessage("Password and it's confirmation are not the same!");
            context.addMessage(component.getClientId(context), mess);
            ((UIInput) component).setValid(false);
        }

    }

在上面的代码component.getAttributes()中,始终返回仅具有两个属性的map: javax.faces.component.VIEW_LOCATION_KEYcom.sun.faces.facelets.MARK_ID.

In above code component.getAttributes() always returns map with only two attributes: javax.faces.component.VIEW_LOCATION_KEY and com.sun.faces.facelets.MARK_ID.

我已经将属性标签添加到h:commandButton进行检查,然后一切都很好.我是否缺少某些内容,或者无法向非操作代码添加属性? 我正在使用Mojarra 2.0.2和Glassfish 3.0.1.

I've added attribute tag to a h:commandButton to check it, and then everything was fine. Am I missing something or it's not possible to add an attribute to non-action tag? I'm using Mojarra 2.0.2 and Glassfish 3.0.1.

谢谢.

推荐答案

输入组件按出现在组件树中的顺序进行处理. UIInput#getValue()仅在组件已被处理时才可用.否则,您需要使用UIInput#getSubmittedValue()代替.

Input components are processed in the order as they appear in the component tree. The UIInput#getValue() is only available when the component is already been processed. Otherwise you need to use UIInput#getSubmittedValue() instead.

<f:attribute name="confirmedPass" value="#{advertAdder.passConfirmator.submittedValue}"/>

请注意,这将为您提供未转换和未经验证的值.将验证器放到确认密码字段上,然后传递第一个密码字段的值,这会更有意义.另请参见将JSF验证程序与字符串进行平等比较 JSF不支持跨域验证,是否有解决方法?

Note that this gives you the unconverted and unvalidated value back. It would make somewhat more sense to put the validator on the confirm password field instead and pass the value of the first password field along. See also JSF Validator compare to Strings for Equality and JSF doesn't support cross-field validation, is there a workaround?

或者,您也可以尝试使用 OmniFaces <o:validateEqual>组件.您可以在本文中找到具体示例>.

Alternatively, you can also try out the OmniFaces <o:validateEqual> component. You can find a concrete example in this article.

无关与具体问题无关,因此不必通过这种方式将组件绑定到Bean.将所有出现的#{advertAdder.passConfirmator}替换为#{passConfirmator}.保持控制器没有内部未使用过的属性.

Unrelated to the concrete problem, it's unnecessary to bind the component to the bean this way. Replace all occurrences of #{advertAdder.passConfirmator} by #{passConfirmator}. Keep the controller free of properties which are never internally used.

这篇关于通过绑定获取的UIComponent#getValue()在另一个组件的验证器中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆