jsf 2 response.sendRedirect不起作用 [英] jsf 2 response.sendRedirect is not working

查看:133
本文介绍了jsf 2 response.sendRedirect不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JSF2.1/Richfaces 4.2应用程序中观察到了一件有趣的事情. Filter(或)JSF Bean操作方法内的所有response.sendRedirect()调用如何都无法正常工作.

I have observed an interesting thing in my JSF2.1/Richfaces 4.2 application. Some how all response.sendRedirect() calls inside either Filter (or) JSF Bean action methods are not working.

如果我们在客户端(浏览器)上使用location.href,则工作正常.

If we use location.href on client side (Browser), it is working fine.

关于可能导致发送重定向失败的任何建议?我已经尝试过以下签名.

Any suggestions on what could be causing send redirect failure? I have tried below signatures.

response.sendRedirect("/appRoot/mypage.xhtml");
  or 
(HttpServletResponse) facesCtx.getResponse()).sendRedirect(((HttpServletRequest)request).getContextPath()+"/appRoot/mypage.faces");

推荐答案

有些方法在Filter(或)JSF Bean操作方法中的所有response.sendRedirect()调用都不起作用.

如果请求实际上是ajax请求,则可能发生这种情况. Web浏览器根本不将XMLHttpRequest上的重定向转换为主窗口.发送ajax请求的JS代码对此负责.它需要一个特殊/不同的响应,以便JS代码理解它需要更改窗口的位置,或者需要更改JS代码本身,以便可以相应地处理响应.

That can happen if the request is actually an ajax request. The webbrowser doesn't translate redirects on XMLHttpRequest to the main window at all. The JS code who's sending the ajax request is responsible for that. It needs either a special/different response so that the JS code understands that it needs to change the window location, or the JS code itself needs to be altered so that it can handle the response accordingly.

如果ajax请求是由JSF本身发起的(例如<f:ajax>等),并且Java代码在JSF上下文中运行,那么您应该使用

If the ajax request is initiated by JSF itself (e.g. <f:ajax> and so on) and the Java code is running inside the JSF context, then you should be using ExternalContext#redirect() instead. This autodetects if the current request is an ajax request and will return a special XML response which tells the JSF ajax engine to change the location of the window. If you're not inside the JSF context (e.g. inside a Filter), then you need to create this special XML response yourself as follows:

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

另请参见:

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