【问题标题】:JSF2.0: variable list of custom componentJSF2.0:自定义组件的变量列表
【发布时间】:2010-06-03 17:22:54
【问题描述】:

是否有任何方法可以将 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) 与 person 对象正确关联,但与支持 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)并相应地对其进行初始化。 这个问题有解决办法吗?

【问题讨论】:

    标签: custom-component jsf-2


    【解决方案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(或者您可以传递人员对象本身)。

    【讨论】:

      猜你喜欢
      • 2011-08-04
      • 2018-01-15
      • 2017-07-06
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      • 2013-09-19
      • 1970-01-01
      • 2019-03-21
      相关资源
      最近更新 更多