【发布时间】:2015-08-08 00:10:19
【问题描述】:
我的 index.xhtml 中有这个可选择的 PrimeFaces 树,带有一个 Ajax 侦听器:
<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 ?
【问题讨论】:
标签: ajax redirect jsf primefaces