【发布时间】:2018-11-16 04:46:59
【问题描述】:
在我正在处理的项目中,我们有一个 LoginFilter,用于重定向未登录的用户。
我在重定向 ajax 请求时遇到了一些问题,但通过使用这种方法使其工作:Authorization redirect on session expiration does not work on submitting a JSF form, page stays the same
现在我正在尝试将参数添加到重定向 URL 的查询字符串,以便登录逻辑可以将用户重定向到正确的视图,因此视图知道它是否应该显示一条消息。这是我的 doFilter 方法的简化版本:
String loginPath = contextPath + "/login.xhtml";
String AJAX_REDIRECT_XML =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<partial-response><redirect url=\"%s\"></redirect></partial-response>";
if(userLoggedIn) {
...
} else if ("partial/ajax".equalsIgnoreCase(httpServletRequest.getHeader("Faces-Request")) {
String queryString = "?destination="
queryString += URLEncoder.encode(contextPath + "/home.xhtml", "UTF-8");
queryString += "&display-message=true";
httpServletResponse.setContentType("text/xml")
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.getWriter().printf(AJAX_REDIRECT_XML, loginPath + queryString);
return;
} else {
...
}
如果我只向查询字符串添加一个参数,则此方法有效,但如果两者都存在,则无效。我得到了正确的 XML 响应,但客户端 javascript 似乎不知道如何处理它?这是从 ajax 请求返回的 XML 示例。
<?xml version="1.0" encoding="UTF-8"?><partial-response><redirect url="/contextPath/login.xhtml?destination=%2FcontextPath%2Fhome.xhtml&display-message=true"></redirect></partial-response>
在浏览器控制台中我收到此错误:
XML Parsing Error: not well-formed
发生这种情况的原因是什么?
【问题讨论】: