【问题标题】:How to access the bean linked in an attribute of UIComponent?如何访问在 UIComponent 的属性中链接的 bean?
【发布时间】:2012-12-13 02:13:43
【问题描述】:

我需要访问在 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 被执行,我需要获取&lt;p:selectBooleanButton value="#{baseBean.selected}"&gt; 中使用的baseBean 对象

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

【问题讨论】:

标签: java validation jsf-2 primefaces el


【解决方案1】:
value="#{baseBean.selected}" should be changed as "#{baseBean.value}"

【讨论】:

    猜你喜欢
    • 2013-09-05
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 2013-09-17
    • 2012-09-03
    相关资源
    最近更新 更多