在外部设置f:param:commandButton(JSF2)

时间:2012-12-08 10:12:25

标签: jsf jsf-2 richfaces facelets

我非常喜欢actionListener以及将整个对象作为参数传递的可能性,而是需要将值传递为String或创建(隐藏)表单字段。我正在使用JSF 2.1(Mojarra)和RichFaces(用于popupPanel)。

目前我遇到了以下问题:

我创建了一个带有按钮的表,用于打开弹出窗口。在该弹出窗口中,用户可以编辑表中当前用户/对象的数据。

当我单击弹出窗口中的按钮以保存编辑时,如何从弹出窗口提交值并告诉bean操作我编辑了哪个userObject?

目前,我的解决方法是在弹出窗口中使用隐藏的inputText字段,但我不喜欢这种方式。还有其他选择吗?

这是我尝试实现的目标(最小化):

<h:datatable value="#{bean.users}" var="user">
  <h:column>
    Username #{user.name}
  </h:column>
  <h:column>
    <input onclick="showPopup()"/>
  </h:column>
</h:datatable>

<rich:popupPanel>
  <h:inputText value="#{bean.text}" />
  <h:commandButton value="Action" actionListener="#{bean.doSomething}">
    <f:attribute name="selected" value="#{userObjectFromDatatable}" />  <-- HOW? -->
  </h:commandButton>
</rich:popupPanel>

1 个答案:

答案 0 :(得分:1)

您可以非常直接地将userObject保留在与@ViewScoped类似的会话范围中。有关@ViewScope的详细信息,请参阅this article。例如,将所需类型的变量声明为辅助bean中的实例变量

  UserObject userObject;
  //getters and setters

在您的表格中,您现在可以使用以下内容在支持bean中设置所选对象

   <h:commandButton value="Action" actionListener="#{bean.doSomething}">
    <f:setPropertyActionListener value="#{user}" target="#{bean.userObject}"/> 
   </h:commandButton>

通过在表中设置辅助bean中的变量,viewscope将确保您对该对象执行的任何其他操作将使用相同的实例,前提是您保持在同一个JSF视图中并且执行不远离它。

相关问题