如何在 bean 的 action 方法中的 UIData 组件中收集 UIInput 组件的值? [英] How to collect values of an UIInput component inside an UIData component in bean's action method?

查看:13
本文介绍了如何在 bean 的 action 方法中的 UIData 组件中收集 UIInput 组件的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 bean 的操作方法期间收集 UIData 组件内的 UIInput 组件的值,以验证重复值.我试图将 UIInput 组件绑定到一个 bean 属性并获取它的值,但它打印了 null.如果我将它放在数据表之外,那么它会打印预期值.数据表有问题吗?

I'm trying to collect values of an UIInput component inside an UIData component during bean's action method in order to validate duplicate values. I tried to bind the UIInput component to a bean property and getting its value, but it prints null. If I place it outside the datatable, then it prints the expected value. Is there something wrong with the datatable?

<rich:dataTable binding="#{bean.table}" value="#{bean.data}" var="item">
    <h:column>
        <f:facet name="header">
            <h:outputText value="Field1" />
        </f:facet>
        <h:inputText binding="#{bean.input}" value="#{item.field1}" />
    </h:column>
</rich:dataTable>

这是支持 bean 代码:

Here's the backing bean code:

private UIData table;
private UIInput input;

public void save() {
    System.out.println(input.getId() + " - " + input.getValue());
}

推荐答案

数据表没有任何问题.JSF 组件树中只有一个 UIInput 组件,每当父 UIData 组件遍历模型的每一项时,其状态都会改变.因此,该状态仅在 UIData 迭代期间可用,而在此之前或之后均不可用.您正在尝试访问 bean 的 action 方法中单个组件的值,而父 UIData 组件并未对其进行迭代,因此这些值将始终返回 null.

There's nothing wrong with the datatable. There's only one UIInput component in the JSF component tree whose state get changed everytime whenever the parent UIData component iterates over every item of the model. The state is thus only available during the UIData iteration and not before or after. You're trying to access the value of a single component in the bean's action method while the parent UIData component isn't iterating over it, so the values will always return null.

需要通过UIComponent#visitTree()UIData 和在 VisitCallback 实现中收集感兴趣的信息.

You need to visit the component tree by UIComponent#visitTree() on the UIData and collect the information of interest in the VisitCallback implementation.

table.visitTree(VisitContext.createVisitContext(FacesContext.getCurrentInstance()), new VisitCallback() {
    @Override
    public VisitResult visit(VisitContext context, UIComponent target) {
        if (target instanceof UIInput) {
            UIInput input = (UIInput) target;
            System.out.println("id: " + input.getId());
            System.out.println("value: " + input.getValue());
        }

        return VisitResult.ACCEPT;
    }
});

顺便说一句,您通常会在 UIInput 组件上使用普通的 Validator 执行验证,或者在这种特殊情况下可能更好,使用 ValueChangeListener.这允许更容易的失效和消息处理.

By the way, you'd normally perform the validation with a normal Validator on the UIInput component or, in this particular case maybe better, a ValueChangeListener. This allows for easier invalidation and message handling.

这篇关于如何在 bean 的 action 方法中的 UIData 组件中收集 UIInput 组件的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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