0

I just want to generate dynamically HtmlInputFields, in this sample i just generated 3 fields. In out.xhtml i want to render these components in with ui:repeat and bind them using binding attribute (not value!!).

The loop.index used with varStatus allways fails when using the binding attribute.

exception:

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

out.xhtml:

<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

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

My Question is now: How do i use bindings with dynamically generated JSF Components properly with ui:repeat in JSF 2.2 ?

Thanx

f.milano
  • 43
  • 5
  • Brief story : For the sake of simplicity, you can avoid the whole mess of the nasty `List` in the backing bean and achieve the same thing using plain simple easy-to-maintain/read/understand XHTML code. – Tiny Aug 23 '15 at 10:43
  • Take a look at [jsf-dynamic-add-remove-components-in-cforeach-loop](http://stackoverflow.com/questions/31963015/jsf-dynamic-add-remove-components-in-cforeach-loop) – Darshan Patel Aug 23 '15 at 10:50
  • @DarshanPatel , thanks. nice example BUT i need to use the binding not the value attribute. – f.milano Aug 23 '15 at 17:05
  • @Tiny: thanks you, but i also like simple easy-to-maintain/read/understandable java code :) – f.milano Aug 23 '15 at 17:06

1 Answers1

1

All binding attributes are evaluated (along with id attribtues and taghandlers like JSTL) during the view build time

the ui:repeat is processed during the Render phase (later).

You shoudn't bind your inputs, you are probably interested in their values, so use a related expression (to the bean) in the Value field

Community
  • 1
  • 1
Nassim MOUALEK
  • 4,702
  • 4
  • 25
  • 44