title: How to navigate / redirect in JSF ajax listener method
body: <p>I have this selectable PrimeFaces Tree with an Ajax listener on selection in my index.xhtml:</p>
  
  <pre><code>&lt;p:tree value="#{seccionesArbolView.root}" var="node" selectionMode="single" &gt;
      &lt;p:treeNode&gt;
          &lt;h:outputText value="#{node.nombre}" /&gt;
      &lt;/p:treeNode&gt;
      &lt;p:ajax event="select" listener="#{seccionesArbolView.onNodeSelect}"&gt;&lt;/p:ajax&gt;
  &lt;/p:tree&gt;                    
  </code></pre>
  
  <p>The listener method is on a session scoped named bean, and should redirect to another page (grupal.xhtml) with some GET parameters. I tried this:</p>
  
  <pre><code>public String onNodeSelect(NodeSelectEvent event) throws IOException {
      Seccion sec = (Seccion) event.getTreeNode().getData();
      String enlace = "grupal.xhtml?faces-redirect=true&amp;id=" + sec.getId() + "&amp;nombre=" + sec.getNombre();
      return enlace;
  }
  </code></pre>
  
  <p>but nothing happens. I read in another question that it might be because of the Ajax petition, so I tried with ExternalContext:</p>
  
  <pre><code>public String onNodeSelect(NodeSelectEvent event) throws IOException {
      Seccion sec = (Seccion) event.getTreeNode().getData();
      FacesContext context = FacesContext.getCurrentInstance();
      String enlace = "faces/grupal.xhtml?id=" + sec.getId() + "&amp;nombre=" + sec.getNombre();
      context.getExternalContext().redirect(enlace);
      context.responseComplete();
  }
  </code></pre>
  
  <p>The problem with this approach is the construction of the link. The way it is done (with the "faces" prefix) works, but everytime the tree is clicked the URL keeps getting bigger, with the addition of another "faces/" (e.g. <a href="http://localhost:8080/MyApp/faces/faces/faces/grupal.xhtml?.." rel="nofollow">http://localhost:8080/MyApp/faces/faces/faces/grupal.xhtml?..</a>.) and Glassfish issues the warning <em>Request path '/faces/faces/grupal.xhtml' begins with one or more occurrences of the FacesServlet prefix path mapping '/faces'</em></p>
  
  <p>If the link is constructed without the "faces/" prefix:</p>
  
  <pre><code>String enlace = "grupal.xhtml?id=" + sec.getId() + "&amp;nombre=" + sec.getNombre();
  </code></pre>
  
  <p>it works, provided that index.html is accessed through the URL <a href="http://localhost:8080/MyApp/faces/index.xhtml" rel="nofollow">http://localhost:8080/MyApp/faces/index.xhtml</a>. But when index.html is accessed through the base URL <a href="http://localhost:8080/MyApp/" rel="nofollow">http://localhost:8080/MyApp/</a>, grupal.xhtml cannot be found, obviously.</p>
  
  <p>I am not sure what is the best solution in this case.</p>
  
  <p>Is there a way to do this with JSF 2.0 navigation (the first approach, that I cannot get to work)? </p>
  
  <p>Is there a way to get the whole base URL to make the redirection with an absolute path?</p>
  
  <p>Is there a way to tell Glassfish to redirect the base URL <a href="http://localhost:8080/MyApp/" rel="nofollow">http://localhost:8080/MyApp/</a> to <a href="http://localhost:8080/MyApp/faces/index.xhtml" rel="nofollow">http://localhost:8080/MyApp/faces/index.xhtml</a> ?</p>