【问题标题】:JSF - include multiple parameters in query string when redirecting an ajax request in a servlet Filter [duplicate]JSF - 在servlet过滤器中重定向ajax请求时在查询字符串中包含多个参数[重复]
【发布时间】: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

发生这种情况的原因是什么?

【问题讨论】:

    标签: jsf servlets


    【解决方案1】:

    所以,我找到了解决方案。

    问题在于查询字符串中的与号。我没有意识到你必须在 XML 中转义那些。

    替换:

    queryString += "&display-message=true";
    

    与:

    queryString += "&amp;display-message=true";
    
    猜你喜欢
    • 2013-11-29
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 2014-09-18
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    相关资源
    最近更新 更多