我有一个JSF 2应用程序,该应用程序有两个页面,一个页面列出学生,另一个页面显示给定学生的详细信息。列表页面在students表的每一行中都有一个指向详细信息页面的链接,单击该链接可在浏览器中打开一个新选项卡以显示这些详细信息。

现在,需求已更改为不再在新选项卡中显示详细信息,而是在列表页面的模式对话框中显示。

我的想法是简单地将详细信息页面内容嵌入到模式对话框中,以使列表页面不会变得太大和难以维护。从这里开始我的怀疑。经过研究后,我将 list 各行中的链接更改为以下按钮:

<p:commandButton value="Details" type="button"
                 onclick="PF('dialog-details').show()">
</p:commandButton>

该对话框声明如下:
<p:dialog widgetVar="dialog-details" header="Details" modal="true" width="95%">
    <ui:include src="student_details.xhtml">
        <ui:param name="id" value="#{student.id}"/>
    </ui:include>
</p:dialog>

最后,详细信息页面更改为如下所示:
<ui:composition
    xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui" xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">

    <f:metadata>
        <f:viewParam name="id" value="#{studentBean.id}" />
    </f:metadata>

    <h1 class="title ui-widget-header ui-corner-all">Details of #{studentBean.bean.name} / #{studentBean.bean.number}</h1>
</ui:composition>

当我单击按钮时,对话框真正显示出来,内容是详细信息页面。我在对话框中看到以下内容:
Details of  /

完全没有错误,但是应该显示的数据却没有。在StudentBean.setId()中设置了一个断点(此方法使用与传递的ID对应的bean实例加载名为Student的属性),但是它从未被击中。

经过一段时间的思考,我开始理解为什么它不起作用。传递到详细信息页面的参数是student.id,但是student是用作var中显示所有学生的<p:datatable/>的名称,因此student<p:dialog/>之外的<p:datatable/>中无效。

因此,我需要的是一种使用给定行中相应学生的ID来显示对话框的方法。理想情况下,我希望在这里进行ajax调用,因此仅在需要时才加载详细信息。

有任何想法吗?

最佳答案

该按钮应该是一个ajax按钮,该按钮设置Bean中当前迭代的实体,然后更新对话框的内容,并最终显示它。该对话框应仅引用bean中的该实体,并在保存时更新列表和表。将对话框放置在主窗体之外并且具有自己的窗体是非常重要的。
这是一个启动示例:

<h:form id="master">
    <p:dataTable value="#{bean.entities}" var="entity">
        <p:column>#{entity.property1}</p:column>
        <p:column>#{entity.property2}</p:column>
        <p:column>#{entity.property3}</p:column>
        ...
        <p:column>
            <p:commandButton value="View" action="#{bean.setEntity(entity)}"
                update=":detail" oncomplete="PF('detail').show()" />
        </p:column>
    </p:dataTable>
</h:form>

<p:dialog id="detail" widgetVar="detail">
    <h:form>
        <p:inputText value="#{bean.entity.property1}" />
        <p:inputText value="#{bean.entity.property2}" />
        <p:inputText value="#{bean.entity.property3}" />
        ...
        <p:button value="Close" onclick="PF('detail').hide(); return false" />
        <p:commandButton value="Save" action="#{bean.save}"
            update=":master" oncomplete="if(!args.validationFailed) PF('detail').hide()" />
    </h:form>
</p:dialog>
@ViewScoped bean中包含以下内容:
private List<Entity> entities; // +getter
private Entity entity; // +getter+setter

@EJB
private EntityService entityService;

@PostConstruct
public void load() {
    entities = entityService.list();
    entity = null;
}

public void save() {
    entityService.save(entity);
    load();
}
也可以看看:
  • Creating master-detail pages for entities, how to link them and which bean scope to choose
  • Creating master-detail table and dialog, how to reuse same dialog for create and edit
  • Keep p:dialog open when a validation error occurs after submit
  • Difference between rendered and visible attributes of <p:dialog>
  • How to display dialog only on complete of a successful form submit
  • 关于jsf - 如何从p :dataTable in a p:dialog and update after save显示当前行的详细信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28236228/

    10-09 16:03
    查看更多