如何访问在UIComponent的属性中链接的bean?

时间:2012-12-27 17:48:04

标签: java validation jsf-2 primefaces el

我需要访问UIComponent的EL表达式中使用的bean对象。

例如,在此示例代码中:

XHTML:

<h:form>
    <f:view>
        <p:selectBooleanButton value="#{baseBean.selected}" onLabel="Instalar" offLabel="Ignorar" onIcon="ui-icon-check" offIcon="ui-icon-close">
            <f:validator validatorId="baseValidator.items" />
        </p:selectBooleanButton>
        <p:commandButton type="submit" value="Submit"
            actionListener="#{baseBean.process}"
            ajax="false" />
    </f:view>
</h:form>

的java:

@FacesValidator("baseValidator.items")
public static class BaseValidator implements Validator
{

    @Override
    public void validate(FacesContext context, UIComponent component,
            Object value) throws ValidatorException {
        ValueReference reference = component.getValueExpression("value").getValueReference(context.getELContext());
        Object o1 = reference.getBase();
        Object o2 = reference.getProperty();
        return; //break point here
    }

}

按下命令按钮后执行BaseValidator.validate,我需要获取baseBean中使用的<p:selectBooleanButton value="#{baseBean.selected}">对象

我的代码目前正在抛出NullPointerException,因为getValueReference正在返回 null 。如何在validate方法中获取该对象?

1 个答案:

答案 0 :(得分:-1)

value="#{baseBean.selected}" should be changed as "#{baseBean.value}"