【问题标题】:Navigate to another page on rowselect of datatable in primefaces导航到primefaces中数据表的rowselect的另一个页面
【发布时间】:2012-05-29 21:33:07
【问题描述】:

我有一个 primefaces 数据表,其中显示了记录数。

I want navigate to another page on the rowSelect event (to edit the selected entity for example).

我能找到的最接近的示例/演示是使用 p:ajax 标记将 rowSelect 事件绑定到侦听器方法 http://www.primefaces.org/showcase-labs/ui/datatableRowSelectionInstant.jsf

我也有一篇同样的文章http://forum.primefaces.org/viewtopic.php?f=3&t=14664

,我尝试和他们一样实现。但它也没有奏效。

我正在尝试这种方式,如果我错过了什么,请指导我。

   <p:dataTable var="product" value="#{addPatientBB.patientAddList}" paginator="true" rows="10" 
             selection="#{addPatientBB.pat}" selectionMode="single"> 

    <p:ajax event="rowSelect" listener="#{addPatientBB.onRowSelect}" />  



        <p:column>  
            <f:facet name="header">  
                <h:outputText value="FirstName" />  
            </f:facet>  
            <h:outputText value="#{product.firstName}" />  
        </p:column>  

          <p:column>  

            <f:facet name="header">  
                <h:outputText value="Email" />  
            </f:facet>  
          <h:outputText value="#{product.email}" />    

           </p:column>  


        <p:column>  
            <f:facet name="header">  
                <h:outputText value="Gender" />  
            </f:facet>  
          <h:outputText value="#{product.gender}" />    

           </p:column> 

     </p:dataTable>

Backing bean 是:

@ManagedBean
@ViewScoped
public class AddPatientBB implements Serializable
{
    private Patient pat;

    public Patient getPat()
    {
    System.out.println("tried to get pat");
    return pat;
    }

    public void setPat(Patient pat)
    {
    this.pat = pat;
    System.out.println("tried to set pat");
    }

    public void onRowSelect()
    {
    System.out.println("inside onRow select  Method");
    ConfigurableNavigationHandler configurableNavigationHandler = (ConfigurableNavigationHandler) FacesContext.getCurrentInstance().getApplication().getNavigationHandler();

    System.out.println("navigation objt created");

    configurableNavigationHandler.performNavigation("Page?faces-redirect=true");

    // Page is my navigation page where I wish to navigate named as
    // "Page.xhtml"

    System.out.println("Navigation executed");

    }

}

那么如何在 rowselect 事件中导航到另一个页面?以及如何在导航表单后显示其值。

我能够进入 onRowSelect() 方法,实际上问题是他无法获取或理解该路径:

configurableNavigationHandler.performNavigation("Page?faces-redirect=true");

因此他无法在此行之后打印任何日志。 为什么会这样?是因为我在使用 Liferay 吗?

请指导我。

【问题讨论】:

  • 你的代码会发生什么?是否执行了 onRowSelect() 方法? bean 中是否设置了选定的 Patient?
  • 不,bean中甚至没有选择患者,也没有执行onrowselect方法
  • 是h:form包围的dataTable吗?
  • 是的,它被 h:form 包围了,我也尝试删除它,但仍然没有得到任何日志,为什么?
  • 它必须被 h:form 包围,所以不要删除它们。尝试将 rowKey 属性添加到数据表中:rowKey="{product.id}"(将 id 替换为类的实际 id 属性)。

标签: ajax jsf-2 primefaces liferay-6


【解决方案1】:

好吧,我认为 onRowSelect 永远不会执行,因为您定义错误。

试试这个:

public void onRowSelect(SelectEvent event)
{


  FacesContext.getCurrentInstance().getExternalContext().redirect("page.xhtml?id=" +pat.getId());


}

【讨论】:

  • 根据 JSF 规范,您必须调用 FacesContext.getCurrentInstance().responseComplete();重定向后。
  • 我不明白你为什么用错误的信息编辑我的答案。来自 de jsf docs 清楚地说:将请求重定向到指定的 URL,并导致在当前请求的 FacesContext 实例上调用 responseComplete() 方法。
  • 查看 jcp.org/en/jsr/detail?id=344 最终版本规范并查看第 2.1.3 章,在第 2-4 页的开头,它清楚地表示“然后有必要告诉 JSF 实现响应有已创建,因此应跳过请求处理生命周期的呈现响应阶段。这是通过在从事件处理程序或应用程序操作返回之前调用当前请求的 FacesContext 实例上的 responseComplete() 方法来完成的。"
  • 你是对的,但是重定向方法自己调用 responseComplete() 所以不需要自己做。见javaserverfaces.java.net/docs/2.2/javadocs/javax/faces/context/…
  • 该文档仅适用于 Mojarra RI,但其他实现可能会有所不同。
【解决方案2】:

如果你使用 FacesServlet,那么使用 .jsf 而不是 .xhtml

public void onRowSelect(SelectEvent event)
{
  FacesContext.getCurrentInstance().getExternalContext().redirect("page.jsf?id="+pat.getId());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 2012-11-02
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多