这很简单,但不幸的是我不了解 JSF 中“幕后”的一些过程。所以,我也对相关文章的链接感兴趣。
问题:我有一个用户对象列表。我将此列表表示为数据表。
<h:dataTable var="user" value="#{userService.allUsers}">
<h:column>
<f:facet name="header">
<h:outputText value="Login"/>
</f:facet>
<h:outputText value="#{user.login}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Birthdate"/>
</f:facet>
<h:outputText value="#{user.birthDate}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Name"/>
</f:facet>
<h:outputText value="#{user.name}"/>
</h:column>
<h:column>
<!--This form with button is not a real code, just my suggestion how these things should be done-->
<h:form>
<h:commandButton action="openEditForm" value="Edit"/>
</h:form>
</h:column>
</h:dataTable>
现在我想将命令按钮“编辑”添加到每个表格行。单击此按钮将打开一些带有可编辑字段的表单,我们可以在其中更改当前值。
<!--It's not a real code, just my suggestion how these things should be done-->
<h:form>
<h:inputText value="#{user.login}"/>
<h:inputText value="#{user.birthDate}"/>
<h:inputText value="#{user.name}"/>
<h:commandButton action="saveEdits" value="Save"/>
</h:form>
所以,我有两个问题:
- 如何将“编辑”按钮绑定(bind)到每一行(即绑定(bind)到用户对象)?
- 如何获取 edit-form 中的 User-object(第二段代码)?
最佳答案
<h:commandLink id="editlink" value="#{msg.edit}" action="myEditPageNavRule">
<f:setPropertyActionListener value="#{user}"
target="#{userBean.user}" />
</a4j:commandLink>
其中 #{user}
是您的数据表变量。根据您的 userBean 的范围,您将在打开编辑页面时获得适当的数据。 (它肯定会在 session 范围内工作,如果你的导航规则没有“重定向”,我认为你将在请求范围内使用它)。
上面的代码只是将当前的User
对象设置为target
属性。
关于java - 如何将表行绑定(bind)到 jsf 中的编辑按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1889124/