JSF:从绿豆上primefaces AJAX事件重定向 [英] jsf: redirecting from bean on primefaces ajax event

查看:447
本文介绍了JSF:从绿豆上primefaces AJAX事件重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的选择PrimeFaces树上选择一个Ajax听众在我的index.xhtml:

 <电话号码:树值=#{seccionesArbolView.root}VAR =节点的SelectionMode =单一>
    <电话号码:treeNode的>
        < H:的outputText值=#{node.nombre}/>
    < / P:treeNode的>
    <电话号码:AJAX事件=选择监听器=#{seccionesArbolView.onNodeSelect}>< / P:AJAX>
< / P:树>
 

监听器的方法是在一个会话范围的bean,并应重定向到另一个页面(grupal.xhtml)与一些GET参数。我想这样的:

 公共字符串onNodeSelect(NodeSelectEvent事件)抛出IOException异常{
    Seccion秒=(Seccion)event.getTreeNode()的getData()。
    串缠绕=?grupal.xhtml面临重定向=真和ID =+ sec.getId()+&安培; NOMBRE =+ sec.getNombre();
    返回缠绕;
}
 

但没有任何反应。我读了另一个问题,这可能是因为Ajax的请愿书,所以我试着用的ExternalContext:

 公共字符串onNodeSelect(NodeSelectEvent事件)抛出IOException异常{
    Seccion秒=(Seccion)event.getTreeNode()的getData()。
    FacesContext的上下文= FacesContext.getCurrentInstance();
    串缠绕=面孔/ grupal.xhtml ID =?+ sec.getId()+&放大器; NOMBRE =+ sec.getNombre();
    。context.getExternalContext()重定向(缠绕);
    context.responseComplete();
}
 

使用这种方法的问题是该链接的结构。事情是这样做(与面preFIX)的作品,但每次树被点击的URL还在不断变大,增加的另一个面孔/(例如: HTTP://本地主机:8080 / MyApp的/面/面/面/ grupal.xhtml .. )和Glassfish发出警告的请求路径/faces/faces/grupal.xhtml'开头的FacesServlet的preFIX路径映射的一次或多次出现/面孔

如果该链接没有面孔/preFIX构造:

 串缠绕=grupal.xhtml ID =?+ sec.getId()+&放大器; NOMBRE =+ sec.getNombre();
 

它的工作原理,但index.html的是通过URL HTTP访问://本地主机:8080 /MyApp/faces/index.xhtml 。但是,当index.html的是通过基本URL HTTP访问://本地主机:8080 / MyApp的/ ,grupal。 XHTML无法找到,效果显着。

我不知道什么是在这种情况下,最好的解决办法。

有没有办法用JSF做2.0的导航(第一种方法,我不能去上班)?

有没有办法让整个基础URL进行重定向的绝对路径?

有没有办法告诉Glassfish的重定向基础URL 的http://本地主机:8080 / MyApp的/ 的http://本地主机:8080 / MyApp的/面/的index.xhtml

解决方案
  

有没有办法用JSF做2.0的导航(第一种方法,我不能去上班)?

您可以通过编程执行导航使用<一个href="http://docs.oracle.com/javaee/7/api/javax/faces/application/NavigationHandler.html#handleNavigation(javax.faces.context.FacesContext,%20java.lang.String,%20java.lang.String)"相对=nofollow> NavigationHandler#handleNavigation()

 公共无效onNodeSelect(NodeSelectEvent事件){
    // ...
    串结果=?grupal.xhtml面临重定向=真和ID =+ sec.getId()+&安培; NOMBRE =+ sec.getNombre();
    FacesContext的上下文= FacesContext.getCurrentInstance();
    。context.getApplication()getNavigationHandler()handleNavigation(背景下,空,结局)。
}
 


  

有没有办法让整个基础URL进行重定向的绝对路径?

您可以用编程方式获取上下文路径<一href="http://docs.oracle.com/javaee/7/api/javax/faces/context/ExternalContext.html#getRequestContextPath()"相对=nofollow> 的ExternalContext#getRequestContextPath()

 公共无效onNodeSelect(NodeSelectEvent事件)抛出IOException异常{
    // ...
    串的uri =/faces/grupal.xhtml?id=+ sec.getId()+&安培; NOMBRE =+ sec.getNombre();
    的ExternalContext EC = FacesContext.getCurrentInstance()getExternalContext()。
    ec.redirect(ec.getRequestContextPath()+ URI);
}
 


  

有没有办法告诉Glassfish的重定向基础URL 的http://本地主机:8080 / MyApp的/ 的http://本地主机:8080 / MyApp的/面/的index.xhtml

使用一个&LT;欢迎-文件&gt; 的web.xml 指数。 XHTML 并更换尴尬的JSF 1.0风格的 FacesServlet的 /映射面临/ * 通过JSF 2.0风格的 *。XHTML 。另请参见 JSF欢迎文件无法识别和<一href="http://stackoverflow.com/questions/3008395/jsf-facelets-sometimes-i-see-the-url-is-jsf-and-sometimes-xhtml-why">JSF Facelets的:有时我看到的网址是.jsf,有时.xhtml。为什么呢?

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>                    

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;
}

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();
}

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'

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

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

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.

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

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

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

解决方案

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

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);
}


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

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);
}


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

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. See also JSF welcome file is not recognized and JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?

这篇关于JSF:从绿豆上primefaces AJAX事件重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆