0

BalusC shows here that in order to do the navigation we can use

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

which is fine, but how to do the same if I don't want an additional column as a navigation - I want to do it onclicking the row.

What I've tried. The list.xhtml:

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

Which goes to bean:

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

and this in fact navigates me to new page after clicking a row in a datatable, the url is:

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

so id of a row on datatable is appended. What happens is of course the NULL:

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

The 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>

Obviously I am doing sth wrong.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Syjmick
  • 51
  • 1
  • 5
  • 1
    `FacesContext.getCurrentInstance().getExternalContext().redirect("edit.xhtml?faces-redirect=true&id=" + listItem.getId()); }` is 'duplicate' . The `faces-redirect=true` is superfluous. And the error means most likely that listcontroller is not found (resolved) – Kukeltje Nov 12 '18 at 19:51
  • Or `ListController.getItem()` returns `null`? – Selaron Nov 13 '18 at 14:06

2 Answers2

0

The onRowSelect() method is an AJAX call (assuming you're using PrimeFaces). In this case, it's difficult to use the standard JSF way for sending redirects, as follows:

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

Instead, you can use ExternalContext and call redirect() on it, as follows:

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

Read more on this on BalusC's answer here: https://stackoverflow.com/a/11277482/3987745

Edward Quixote
  • 350
  • 5
  • 16
-1
    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());
}
theeuller
  • 51
  • 1
  • 8