简体   繁体   English

JSF:嵌套ui:repeat中的值绑定

[英]JSF: value bindings inside nested ui:repeat

I have a strange problem with the <ui:repeat> tag. 我在<ui:repeat>标记中遇到了一个奇怪的问题。 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>

and a Sandbox class: 和一个沙箱类:

@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");
    }
}

So my facelet loops over the "rows" in sandbox, which has a number of "columns", which each has a value. 因此,我的facelet在沙箱中的“行”上循环,沙箱中有许多“列”,每个“列”都有一个值。 For each such column, the value is printed, and a <h:selectBooleanCheckbox> with the value bound to it is output. 对于每个这样的列,将打印该值,并输出绑定了值的<h:selectBooleanCheckbox>

When I load the page, all values are displayed as false, and all checkboxes are unchecked. 加载页面时,所有值均显示为false,并且所有复选框均未选中。 Now, clicking refresh is supposed to alter the value of the first column of the first row to true. 现在,应该单击“刷新”将第一行的第一列的值更改为true。 However, I get the following output: 但是,我得到以下输出:

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

In other words, the <h:outputText> displays it as true, but the checkbox is not checked. 换言之,所述<h:outputText>显示它作为真实的,但该复选框选中。 Certainly I am allowed to change the model in the invoke application phase and that should be reflected when rendering the view? 当然可以在调用应用程序阶段更改模型,并且在呈现视图时应该反映出来吗?

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. 如果我删除了一层嵌套,那么只有一个<ui:repeat> ,一切都会按预期进行:选中该复选框,并且该值显示为true。 So this appears to have something to do with the UIRepeat component. 因此,这似乎与UIRepeat组件有关。 In fact, it seems like UIRepeat has some special handling for when it's nested inside another UIRepeat. 实际上,当UIRepeat嵌套在另一个UIRepeat中时,似乎有一些特殊的处理方法。

From what I gather, UIRepeat essentially rerenders the same component several times. 根据我的收集,UIRepeat本质上重新渲染了相同的组件几次。 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). 在每次渲染调用之间,它将从内部映射(键入渲染的组件的实际ID)上加载实现EditableValueHolder的所有子组件的“状态”(值,localValue,submittedValue)。 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. 我已经尝试过在发生这种情况时放置断点,以跟踪将哪些值插入到已保存状态的映射中,但这确实是一团糟,因为saveChildState和restoreChildState方法被调用了无数次。

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. 我真正需要的是能够呈现一个可以在水平和垂直方向上动态增长的表,其中包含复选框,输入字段等。我瞥了一眼<h:dataTable>但我相信在这种情况下它不起作用。

Interesting issue. 有趣的问题。 I can reproduce this with Mojarra 2.0.3. 我可以在Mojarra 2.0.3中重现它。 It's definitely a problem in state saving of ui:repeat . 在保存ui:repeat状态中绝对是一个问题。 I've reported it as issue 1807 to the Mojarra guys. 我已经向Mojarra的人报告了1807年问题 It works by the way fine when the outer loop is a c:forEach . 当外部循环是c:forEach时,它可以很好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM