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

问题描述 投票:1回答:1

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

<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)。我试过了:

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:

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']开始]]

如果链接没有前缀“ faces /”:

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

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

我不确定在这种情况下最好的解决方案是什么。

是否可以使用JSF 2.0导航来做到这一点(第一种方法,我无法上班?)

是否有一种方法可以获取整个基本URL并使用绝对路径进行重定向?

是否有办法告诉Glassfish将基本URL http://localhost:8080/MyApp/重定向到http://localhost:8080/MyApp/faces/index.xhtml

我有这个可选择的PrimeFaces树,在我的index.xhtml中具有选择时的Ajax侦听器:... []]

有没有一种方法可以使用JSF 2.0导航(第一种方法,我无法上班)?
您可以使用NavigationHandler#handleNavigation()以编程方式执行导航。

NavigationHandler#handleNavigation()


有没有一种方法可以获取整个基本URL并使用绝对路径进行重定向?
您可以使用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); } 以编程方式获取上下文路径。

ExternalContext#getRequestContextPath()


有没有办法告诉Glassfish将基本URL 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); }

http://localhost:8080/MyApp/http://localhost:8080/MyApp/faces/index.xhtml中使用<welcome-file>,并用JSF 2.0样式web.xml替换index.xhtml的笨拙的JSF 1.0样式FacesServlet映射。另请参见/faces/**.xhtml

ajax redirect jsf primefaces
1个回答
1
投票
有没有一种方法可以使用JSF 2.0导航(第一种方法,我无法上班)?
© www.soinside.com 2019 - 2024. All rights reserved.