views:

45

answers:

1

Hello
Is there any way of using JSF2.0 in connection with variable lists of components? For example, lets say I have list o people that I would like to edit. They are presented on the page as list of components PersonEditor, which allow changing person data. Each editor is associated with single Person element. In order for this to work I need to perform following steps:

On initial request:

  1. Get list of people
  2. For each person create PersonEditor and associate it with Person object.
  3. Fill editor's data.

On user action:

  1. When user changes values and presses Save, data is processed by backing bean.

I can either fill editor with data from list of people or bind it to the backing bean, but not at the same time, so I am stuck.

I tried
people.xhtml

<ui:render value="#{bean.people}" var="person">
  <example:personEditor person="#{person}"/>
</ui:render>

where personEditor.xhtml:
a) proper association with person object, but no connection to backing bean

<h:form>
  <h:outputText value="#{cc.attr.person.name}"/>
  <h:commandButton name="Save" actionListener="editorBean.save">
    <f:ajax execute="@form" render="@form"/>
  </h:commandButton>
</h:form>

b) no association with person object, but there is connection to backing bean - there is no way to pass that person to the backing bean

<h:form>
  <h:outputText value="#{editorBean.name}"/>
  <h:commandButton name="Save" actionListener="editorBean.save">
    <f:ajax execute="@form" render="@form"/>
  </h:commandButton>
</h:form>

If I had each editor on separate page, I could pass the person id as url parameter (either using f:param or f:attribute) and initialize it accordingly. Is there any solution to this problem?

A: 

Hm, wondering why nobody answered this so far... Check this out:

http://balusc.blogspot.com/2006/06/communication-in-jsf.html

So your code would look something like this:

<h:form>
  <h:outputText value="#{cc.attr.person.name}"/>
  <h:commandButton name="Save" actionListener="#{editorBean.save}">
    <f:ajax execute="@form" render="@form"/>
    <f:setPropertyActionListener target="#{editorBean.editedPersonId}" value="#{cc.attr.person.id}" />
  </h:commandButton>
</h:form>

and upon calling editorBean.save the attribute will contain the id of the person edited (or you could pass the person object itself).

Gabor Kulcsar