JSF:嵌套ui:repeat中的值绑定 [英] JSF: value bindings inside nested ui:repeat

查看:107
本文介绍了JSF:嵌套ui:repeat中的值绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<ui:repeat>标记有一个奇怪的问题.即使对于我的非常简单的示例,嵌套重复组件内的值绑定也无法按预期工作.

I have a strange problem with the <ui:repeat> tag. Even for my very simple example, value bindings inside nested repeat components do not work as expected.

我有一个像这样的简单面:

I have a simple facelet like so:

<h:body>
<h:form>
<ui:repeat value="#{sandbox.rows}" var="row">
    <ui:repeat value="#{row.columns}" var="column">
        <h:outputText value="#{column.value}" />
        <h:selectBooleanCheckbox value="#{column.value}" />
    </ui:repeat>
    <br/>
</ui:repeat>

<h:commandButton action="#{sandbox.refresh}" value="Refresh" />
</h:form>
</h:body>

和一个沙箱类:

@Component
@Scope("request")
public class Sandbox {

    public static class Row {
        private List<Column> columns = Arrays.asList(new Column(), new Column());
        public List<Column> getColumns() {
            return columns;
        }
    }

    public static class Column {
        private boolean value;
        public void setValue(boolean value) {
            this.value = value;
        }
        public boolean getValue() {
            return this.value;
        }
    }

    public List<Row> rows = Arrays.asList(new Row(), new Row()); 

    public List<Row> getRows() {
        return rows;
    }

    public void refresh() {
        rows.get(0).getColumns().get(0).setValue(true);
        System.err.println("refresh clicked");
    }
}

因此,我的facelet在沙箱中的行"上循环,沙箱中有许多列",每个列"都有一个值.对于每个这样的列,将打印该值,并输出绑定了该值的<h:selectBooleanCheckbox>.

So my facelet loops over the "rows" in sandbox, which has a number of "columns", which each has a value. For each such column, the value is printed, and a <h:selectBooleanCheckbox> with the value bound to it is output.

当我加载页面时,所有值都显示为false,并且所有复选框均未选中.现在,单击刷新应该会将第一行的第一列的值更改为true.但是,我得到以下输出:

When I load the page, all values are displayed as false, and all checkboxes are unchecked. Now, clicking refresh is supposed to alter the value of the first column of the first row to true. However, I get the following output:


true [ ] false [ ]
false [ ] false [ ]

换句话说,<h:outputText>将其显示为true,但未选中复选框.当然可以在调用应用程序阶段更改模型,并且在呈现视图时应该反映出来吗?

In other words, the <h:outputText> displays it as true, but the checkbox is not checked. Certainly I am allowed to change the model in the invoke application phase and that should be reflected when rendering the view?

如果我删除了一层嵌套,那么只有一个<ui:repeat>,那么一切都会按预期进行:选中该复选框,并且该值显示为true.因此,这似乎与UIRepeat组件有关.实际上,当UIRepeat嵌套在另一个UIRepeat中时,似乎有一些特殊的处理方法.

If I remove one level of nesting, so there is only one <ui:repeat>, everything works as expected: the checkbox is checked and the value is displayed as true. So this appears to have something to do with the UIRepeat component. In fact, it seems like UIRepeat has some special handling for when it's nested inside another UIRepeat.

从我收集到的信息来看,UIRepeat实际上多次重现了相同的组件.在每次渲染调用之间,它会从内部映射(键入渲染的组件的实际ID)上加载实现EditableValueHolder的所有子组件的状态"(值,localValue,submittedValue).我尝试过在发生这种情况时放置断点,以跟踪在已保存状态的映射中插入了什么值,但这确实是一团糟,因为saveChildState和restoreChildState方法的调用时间约为一千亿次.

From what I gather, UIRepeat essentially rerenders the same component several times. Between each call to render, it loads the "state" (value, localValue, submittedValue) of all child components implementing EditableValueHolder from an internal map (keyed on the rendered component's actual id). I've tried putting break points when this occurs to track what value is inserted into the map of saved states, but it's really a mess, as the saveChildState and restoreChildState methods are called about a gazillion times.

有什么想法吗?我可以做些不同的事情吗?我真正需要的是能够呈现一个可以在水平和垂直方向上动态增长的表,其中包含复选框,输入字段等.我瞥了一眼<h:dataTable>,但是我相信在这种情况下它是行不通的.

Any ideas? Can I go about this differently? What I really need is be able to render a table that grows dynamically both horizontally and vertically, containing checkboxes, input fields, etc. I glanced at <h:dataTable> but I believe it won't work in this case.

推荐答案

有趣的问题.我可以在Mojarra 2.0.3中重现它. ui:repeat的状态保存绝对是一个问题.我已经向Mojarra的人报告了第1807 页.当外部循环为c:forEach时,它可以很好地工作.

Interesting issue. I can reproduce this with Mojarra 2.0.3. It's definitely a problem in state saving of ui:repeat. I've reported it as issue 1807 to the Mojarra guys. It works by the way fine when the outer loop is a c:forEach.

这篇关于JSF:嵌套ui:repeat中的值绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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