关于jsf 2.2:JSF 2.2:ui:使用varStatus动态生成的HtmlInputText重复失败

JSF 2.2: ui:repeat with dynamically generated HtmlInputText using varStatus failed

本问题已经有最佳答案,请猛点这里访问。

我只想动态生成HtmlInputFields,在此示例中,我仅生成了3个字段。在out.xhtml中,我想使用ui:repeat渲染这些组件,并使用绑定属性(不是值!)绑定它们。

使用绑定属性时,始终与varStatus一起使用的loop.index失败。

例外:

1
binding="#{loop.index}": Target Unreachable, identifier 'loop' resolved to null

out.xhtml:

1
2
3
4
5
6
7
8
9
10
<ui:repeat value="#{myBean.htmlInputs}" varStatus="loop" var="bItem">
  <!-- THIS WORKS -->
  <h:inputText value="#{loop.index}" />
  <!-- THIS WORKS -->
  <h:inputText value="#{myBean.htmlInputs[0]}" />
  <!-- THIS WORKS ALSO -->
  <h:inputText binding="#{myBean.htmlInputs[0]}" />
  <!-- AND THIS FAILES ?? WHY ?? -->
  <h:inputText binding="#{myBean.htmlInputs[loop.index]}" /><p/>
</ui:repeat>

MyBean.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Named
@SessionScoped
public class BookingBean implements Serializable {
  private List<HtmlInputText> htmlInputs = new ArrayList<>();

  @PostConstruct
  public void init() {
    HtmlInputText hInput;
    for (int i=0 ; i<3 ; i++) {
      hInput = new HtmlInputText();
      hInput.setValue("item #:" + i);
      htmlInputs.add( hInput );
    }
  }

  public List<HtmlInputText> getHtmlInputs() {
    return htmlInputs;
  }

  public void setHtmlInputs(List<HtmlInputText> htmlInputs) {
    this.htmlInputs = htmlInputs;
  }
}

我的问题是:
如何在JSF 2.2中将ui:repeat与动态生成的JSF组件正确地使用绑定?

感谢


在视图构建期间评估所有绑定属性(以及id属性和JSTL之类的标记处理程序)

ui:repeat在渲染阶段(稍后)进行处理。

您不应绑定您的输入,您可能会对它们的值感兴趣,因此请在"值"字段中使用一个相关的表达式(对bean)