问题描述
在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);
}
另请参见:
- 如何使用导航规则进行重定向
- How to make redirect using navigation-rule
See also:
您可以使用 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()不会重定向到父目录
- ExternalContext#redirect() does not redirect to parent directory
- JSF welcome file is not recognized
- Sometimes I see JSF URL is *.jsf, sometimes *.xhtml and sometimes /faces/*. Why?
See also:
在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 Ajax侦听器方法中导航/重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!