使用JSF 2.0 / Facelets的,是有办法附加一个全球性监听所有的AJAX调用? [英] Using JSF 2.0 / Facelets, is there a way to attach a global listener to all AJAX calls?

查看:189
本文介绍了使用JSF 2.0 / Facelets的,是有办法附加一个全球性监听所有的AJAX调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法附加一个全球性监听到所有的JSF AJAX调用?也许通过一个阶段侦听器什么的?

下面是难题......让你在使用F说:AJAX标签和类似阿帕奇四郎,你让你的会话过期。然后你回来,然后点击一个按钮,有一个f:连接到它的ajax。该服务器将响应302重定向到登录页面。

用户什么也看不见。他们可以多次点击,并调用Ajax调用,但对他们的应用就是死了。

所以,我虽然是,有没有办法来侦听器附加到JSF所有的Ajax调用?如果是这样,我想什么做的是监测响应code。如果它是一个重定向,使用window.navigate他们跟随自己的意愿发送。

我总是打开听听其他人如何解决这个问题! 谢谢!

解决方案
  

有没有办法附加一个全球性监听到所有的JSF AJAX调用?也许通过一个阶段侦听器什么的?

是的,<一个href="http://docs.oracle.com/javaee/6/api/javax/faces/event/PhaseListener.html"><$c$c>PhaseListener可以做到。 A <一个href="http://docs.oracle.com/javaee/6/api/javax/faces/event/SystemEventListener.html"><$c$c>SystemEventListener也。一个 过滤器

如果您是JSF的上下文中,那么你可以检查如下当前请求是否是一个Ajax请求。

 如果(FacesContext.getCurrentInstance()。getPartialViewContext()。isAjaxRequest()){
    //这是一个Ajax请求。
}
 

如果你不JSF上下文中,例如在过滤器,那么你可以检查如下当前请求是否是一个JSF的AJAX请求。

 如果(分/ AJAX.equals(request.getHeader(面孔请求))){
    //这是一个JSF Ajax请求。
}
 


  

这里是难题......让你在使用F说:AJAX标签和类似阿帕奇四郎,你让你的会话过期。然后你回来,然后点击一个按钮,有一个f:连接到它的ajax。该服务器将响应302重定向到登录页面。

     

用户什么也看不见。他们可以多次点击,并调用Ajax调用,但对他们的应用就是死了。

这是一个Ajax请求强制重定向需要特殊的XML响应。当你的JSF上下文中,那么<一个href="http://docs.oracle.com/javaee/6/api/javax/faces/context/ExternalContext.html#redirect%28java.lang.String%29"><$c$c>ExternalContext#redirect()已经隐含需要考虑到这一点。所有你需要做的是这样写:

  FacesContext.getCurrentInstance()getExternalContext()重定向(URL);
 

如果你不JSF上下文中,例如在过滤器,那么你就需要自己编写整个XML响应。例如,

  response.setContentType(为text / xml);
response.getWriter()
    .append(&所述;?xml的版本= \1.0 \编码= \UTF-8 \&GT;?)
    .printf(&LT;部分响应&GT;&LT;重定向URL = \%s \的&GT;&LT; /重定向&GT;&LT; /部分响应&gt;中,URL);
 

Is there a way to attach a global listener to all AJAX calls in JSF? Maybe through a phase listener or something?

Here is the conundrum... Lets say you're using f:ajax tags and something like apache shiro and you let your session expire. Then you come back and click a button that has an f:ajax attached to it. The server will respond with a 302 redirect to the login page.

The user sees nothing. They can repeatedly click and invoke the ajax call, but to them the app is just "dead."

So, my though is, is there a way to attach a listener to all ajax calls in JSF? If so, what I'd like to do is monitoring the response code. If it's a redirect, use a window.navigate to send them along their way.

I'm always open to hear how others have solved this problem! Thanks!

解决方案

Is there a way to attach a global listener to all AJAX calls in JSF? Maybe through a phase listener or something?

Yes, a PhaseListener can do it. A SystemEventListener also. A Filter also.

If you're inside JSF context, then you can check as follows whether the current request is an ajax request or not.

if (FacesContext.getCurrentInstance().getPartialViewContext().isAjaxRequest()) {
    // It's an ajax request.
}

If you're not inside JSF context, e.g. inside a Filter, then you can check as follows whether the current request is a JSF ajax request or not.

if ("partial/ajax".equals(request.getHeader("Faces-Request"))) {
    // It's a JSF ajax request.
}


Here is the conundrum... Lets say you're using f:ajax tags and something like apache shiro and you let your session expire. Then you come back and click a button that has an f:ajax attached to it. The server will respond with a 302 redirect to the login page.

The user sees nothing. They can repeatedly click and invoke the ajax call, but to them the app is just "dead."

Forcing a redirect on an ajax request requires a special XML response. When you're inside JSF context, then ExternalContext#redirect() already takes this implicitly into account. All you need to do is to write this:

FacesContext.getCurrentInstance().getExternalContext().redirect(url);

If you're not inside JSF context, e.g. inside a Filter, then you'd need to write the whole XML response yourself. E.g.

response.setContentType("text/xml");
response.getWriter()
    .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
    .printf("<partial-response><redirect url=\"%s\"></redirect></partial-response>", url);

这篇关于使用JSF 2.0 / Facelets的,是有办法附加一个全球性监听所有的AJAX调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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