开发者

JSF2.0: variable list of custom component

开发者 https://www.devze.com 2023-01-02 08:58 出处:网络
Is there any wa开发者_C百科y 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 li

Is there any wa开发者_C百科y 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?


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号