0

有没有办法将 JSF2.0 与组件的可变列表结合使用?例如,假设我列出了我想编辑的人。它们在页面上显示为组件 PersonEditor 的列表,允许更改人员数据。每个编辑器都与单个 Person 元素相关联。为了使其工作,我需要执行以下步骤:

根据初始请求:

  1. 获取人员列表
  2. 为每个人创建 PersonEditor 并将其与 Person 对象相关联。
  3. 填写编辑器的数据。

关于用户操作:

  1. 当用户更改值并按下 Save 时,数据由 backing bean 处理。

我可以用人员列表中的数据填充编辑器,也可以将其绑定到支持 bean,但不能同时进行,所以我被卡住了。

我试过
people.xhtml

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

其中personEditor.xhtml:
a)与人对象正确关联,但与支持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) 与 person 对象没有关联,但与 backing bean 有联系 - 无法将该 person 传递给 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>

如果我将每个编辑器放在单独的页面上,我可以将人员 ID 作为 url 参数传递(使用 f:param 或 f:attribute)并相应地对其进行初始化。这个问题有什么解决办法吗?

4

1 回答 1

1

嗯,想知道为什么到目前为止没有人回答这个问题......看看这个:

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

所以你的代码看起来像这样:

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

并且在调用 editorBean.save 时,该属性将包含被编辑人员的 id(或者您可以传递人员对象本身)。

于 2010-07-31T08:27:41.860 回答