【问题标题】:How to navigate from master to details view on rowSelect event in JSF如何在 JSF 中的 rowSelect 事件上从主视图导航到详细视图
【发布时间】:2019-04-15 13:34:48
【问题描述】:

BalusC 显示 here 我们可以使用导航来进行导航

    <h:column>
        <h:link value="Edit" outcome="/products/edit">
            <f:param name="id" value="#{product.id}" />
        </h:link>
    </h:column>

这很好,但是如果我不想要一个额外的列作为导航,如何做同样的事情 - 我想在点击行时这样做。

我尝试过的。 list.xhtml:

<p:dataTable id="datalist" ....>
     <p:ajax event="rowSelect" listener="#{listController.onRowSelect}" />  

哪个去豆:

public void onRowSelect(SelectEvent event) throws IOException {
     FacesContext.getCurrentInstance().getExternalContext().redirect("edit.xhtml?faces-redirect=true&id=" + listItem.getId());
}

这实际上是在单击数据表中的一行后将我导航到新页面,网址是:

http://localhost/app/faces/edit.xhtml?faces-redirect=true&id=1686

附加数据表上一行的id。 发生的当然是NULL:

javax.el.PropertyNotFoundException: /edit.xhtml @18,84 value="#{listController.item.id}": Target Unreachable, 'null' returned null

edit.xhtml:

   <ui:composition template="/template.xhtml">
    <ui:define name="body">            
        <h:form id="itemForm">               
            <f:metadata>
                <f:viewParam name="id" value="#{listController.item.id}" />
            </f:metadata>

显然我做错了什么。

【问题讨论】:

  • FacesContext.getCurrentInstance().getExternalContext().redirect("edit.xhtml?faces-redirect=true&amp;id=" + listItem.getId()); } 是“重复”。 faces-redirect=true 是多余的。并且该错误意味着很可能未找到(已解决)列表控制器
  • 或者ListController.getItem()返回null

标签: ajax jsf navigation


【解决方案1】:

onRowSelect() 方法是一个 AJAX 调用(假设您使用的是 PrimeFaces)。在这种情况下,很难使用标准的 JSF 方式发送重定向,如下:

public String submit() {
    return "/teams_detail.xhtml?faces-redirect=true";
}

相反,您可以使用ExternalContext 并在其上调用redirect(),如下所示:

ExternalContext ecoxExternalContext = FacesContext.getCurrentInstance().getExternalContext();
ecoxExternalContext.redirect(ecoxExternalContext.getRequestContextPath() + "/teams-flow/team_detail.xhtml");

在此处阅读有关 BalusC 答案的更多信息:https://stackoverflow.com/a/11277482/3987745

【讨论】:

    【解决方案2】:
        public void onRowSelect(SelectEvent event) throws IOException {
         setItem(listItem); //you need to set the item before you try to access
         FacesContext.getCurrentInstance().getExternalContext().redirect("edit.xhtml?faces-redirect=true&id=" + listItem.getId());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-29
      • 1970-01-01
      • 1970-01-01
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      • 2013-05-09
      相关资源
      最近更新 更多