使用 JSF 2.0/Facelets,有没有办法将全局侦听器附加到所有 AJAX 调用? [英] Using JSF 2.0 / Facelets, is there a way to attach a global listener to all AJAX calls?

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

问题描述

有没有办法将全局侦听器附加到 JSF 中的所有 AJAX 调用?也许通过相位监听器之类的?

这是一个难题...假设您正在使用 f:ajax 标签和诸如 apache shiro 之类的东西,并且您让会话过期.然后您返回并单击一个附加了 f:ajax 的按钮.服务器将响应 302 重定向到登录页面.

用户什么也看不到.他们可以反复单击并调用 ajax 调用,但对他们来说,应用程序只是死了".

那么,我的看法是,有没有办法将侦听器附加到 JSF 中的所有 ajax 调用?如果是这样,我想做的是监视响应代码.如果是重定向,请使用 window.navigate 沿途发送它们.

我总是乐于了解其他人是如何解决这个问题的!

解决方案

有没有办法将全局侦听器附加到 JSF 中的所有 AJAX 调用?也许通过相位监听器之类的?

是的,PhaseListener 可以做到.SystemEventListener还.一个 Filter 也是.

如果你在JSF上下文中,那么你可以如下检查当前请求是否是ajax请求.

if (FacesContext.getCurrentInstance().getPartialViewContext().isAjaxRequest()) {//这是一个ajax请求.}

如果您不在 JSF 上下文中,例如在一个Filter中,可以如下检查当前请求是否为JSF ajax请求.

if ("partial/ajax".equals(request.getHeader("Faces-Request"))) {//这是一个 JSF ajax 请求.}


<块引用>

这是一个难题...假设您正在使用 f:ajax 标签和诸如 apache shiro 之类的东西,并且您让会话过期.然后您返回并单击一个附加了 f:ajax 的按钮.服务器将响应 302 重定向到登录页面.

用户什么也看不到.他们可以反复单击并调用 ajax 调用,但对他们来说,应用程序只是死了".

对 ajax 请求强制重定向需要特殊的 XML 响应.当您在 JSF 上下文中时,然后 ExternalContext#redirect() 已经隐含地考虑到了这一点.你需要做的就是写这个:

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

如果您不在 JSF 上下文中,例如在 Filter 中,那么您需要自己编写整个 XML 响应.例如

response.setContentType("text/xml");response.getWriter().append("").printf("<partial-response><redirect url="%s"></redirect></partial-response>", 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!

解决方案

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天全站免登陆