本文介绍了如何在JSF Ajax侦听器方法中导航/重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在index.xhtml中,我选择了这个可选择的PrimeFaces树,并带有一个Ajax侦听器:

I have this selectable PrimeFaces Tree with an Ajax listener on selection in my index.xhtml:

<p:tree value="#{seccionesArbolView.root}" var="node" selectionMode="single" >
    <p:treeNode>
        <h:outputText value="#{node.nombre}" />
    </p:treeNode>
    <p:ajax event="select" listener="#{seccionesArbolView.onNodeSelect}"></p:ajax>
</p:tree>                    

该侦听器方法位于一个名为bean的会话范围内,并且应使用一些GET参数重定向到另一个页面(grupal.xhtml).我试过了:

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:

public String onNodeSelect(NodeSelectEvent event) throws IOException {
    Seccion sec = (Seccion) event.getTreeNode().getData();
    String enlace = "grupal.xhtml?faces-redirect=true&id=" + sec.getId() + "&nombre=" + sec.getNombre();
    return enlace;
}

但是什么也没发生.我在另一个问题中读到它可能是由于Ajax请求所致,所以我尝试使用ExternalContext:

but nothing happens. I read in another question that it might be because of the Ajax petition, so I tried with ExternalContext:

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() + "&nombre=" + sec.getNombre();
    context.getExternalContext().redirect(enlace);
    context.responseComplete();
}

此方法的问题是链接的构造.它的完成方式(带有"faces"前缀)有效,但是每次单击树时,URL都会变得越来越大,并添加了另一个"faces/"(例如 http://localhost:8080/MyApp/faces/faces/faces/grupal.xhtml?.. .)并且Glassfish发出警告请求路径'/faces/faces/grupal.xhtml'以出现一次或多次FacesServlet前缀路径映射'/faces

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. http://localhost:8080/MyApp/faces/faces/faces/grupal.xhtml?...) and Glassfish issues the warning Request path '/faces/faces/grupal.xhtml' begins with one or more occurrences of the FacesServlet prefix path mapping '/faces'

如果链接的构建没有前缀"faces/":

If the link is constructed without the "faces/" prefix:

String enlace = "grupal.xhtml?id=" + sec.getId() + "&nombre=" + sec.getNombre();

它可以工作,只要通过URL http://localhost:8080访问index.html /MyApp/faces/index.xhtml .但是,当通过基本URL http://localhost:8080/MyApp/访问index.html时,grupal.显然,找不到xhtml.

it works, provided that index.html is accessed through the URL http://localhost:8080/MyApp/faces/index.xhtml. But when index.html is accessed through the base URL http://localhost:8080/MyApp/, grupal.xhtml cannot be found, obviously.

我不确定在这种情况下最好的解决方案.

I am not sure what is the best solution in this case.

有没有一种方法可以通过JSF 2.0导航来完成(第一种方法,我无法上班)?

Is there a way to do this with JSF 2.0 navigation (the first approach, that I cannot get to work)?

有没有办法获取整个基本URL并使用绝对路径进行重定向?

Is there a way to get the whole base URL to make the redirection with an absolute path?

有没有一种方法可以告诉Glassfish重定向基本URL http://localhost:8080/MyApp/ http://localhost:8080/MyApp/faces/index.xhtml 吗?

Is there a way to tell Glassfish to redirect the base URL http://localhost:8080/MyApp/ to http://localhost:8080/MyApp/faces/index.xhtml ?

推荐答案

return enlace;

但什么也没发生.

您确实可以listener/actionListener方法进行导航.您只能从action方法进行导航.

You can indeed not navigate from listener/actionListener methods. You can only navigate from action methods.


您可以使用 NavigationHandler#handleNavigation() .

You can programmatically perform navigation using NavigationHandler#handleNavigation().

public void onNodeSelect(NodeSelectEvent event) {
    // ...
    String outcome = "grupal.xhtml?faces-redirect=true&id=" + sec.getId() + "&nombre=" + sec.getNombre();
    FacesContext context = FacesContext.getCurrentInstance();
    context.getApplication().getNavigationHandler().handleNavigation(context, null, outcome);
}

另请参见:

  • 如何使用导航规则进行重定向
  • See also:

    • How to make redirect using navigation-rule

    • 您可以使用 ExternalContext#getRequestContextPath() .

      You can programmatically obtain context path using ExternalContext#getRequestContextPath().

      public void onNodeSelect(NodeSelectEvent event) throws IOException {
          // ...
          String uri = "/faces/grupal.xhtml?id=" + sec.getId() + "&nombre=" + sec.getNombre();
          ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
          ec.redirect(ec.getRequestContextPath() + uri);
      }
      

      另请参见:

10-14 21:27