如何在JSF Ajax侦听器方法中导航/重定向

编程入门 行业动态 更新时间:2024-10-10 21:29:22
本文介绍了如何在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/"(例如 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. 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 localhost:8080访问index.html /MyApp/faces/index.xhtml .但是,当通过基本URL localhost:8080/MyApp/访问index.html时,grupal.显然,找不到xhtml.

it works, provided that index.html is accessed through the URL localhost:8080/MyApp/faces/index.xhtml. But when index.html is accessed through the base URL 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 localhost:8080/MyApp/到 localhost:8080/MyApp/faces/index.xhtml 吗?

Is there a way to tell Glassfish to redirect the base URL localhost:8080/MyApp/ to 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.

  • action和actionListener之间的差异

是否可以使用JSF 2.0导航来完成此操作(第一种方法,我无法上班)?

您可以使用 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
    • 是否可以获取整个基本URL并使用绝对路径进行重定向?

      您可以使用 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); }

      另请参见:

      • ExternalContext#redirect()不会重定向到父目录
      • See also:

        • ExternalContext#redirect() does not redirect to parent directory
        • 是否可以告诉Glassfish重定向基本URL localhost:8080/MyApp /到 localhost:8080/MyApp/faces/index.xhtml 吗?

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

          在index.xhtml的web.xml中使用<welcome-file>,并用JSF 2.0样式*.xhtml替换/faces/*的笨拙的JSF 1.0样式FacesServlet映射.

          Use a <welcome-file> in web.xml on index.xhtml and replace the awkward JSF 1.0 style FacesServlet mapping of /faces/* by JSF 2.0 style *.xhtml.

          • 无法识别JSF欢迎文件
          • 有时我看到了JSF URL是* .jsf,有时是* .xhtml,有时是/faces/*.为什么?
          • JSF welcome file is not recognized
          • Sometimes I see JSF URL is *.jsf, sometimes *.xhtml and sometimes /faces/*. Why?

更多推荐

如何在JSF Ajax侦听器方法中导航/重定向

本文发布于:2023-11-24 19:41:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626558.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:侦听器   重定向   方法   如何在   Ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!