3

我正在使用 JSF2.2 并配置了 servlet 过滤器。Filter 中有效的部分代码:

HttpServletResponse response = (HttpServletResponse) resp;
if (userSession == null) {

    redirectURLRegular = response.encodeRedirectURL("../login.xhtml?param1=noSession");
    redirectURLAjax = response.encodeRedirectURL(request.getContextPath()
                + "/faces/login.xhtml?param1=noSession");

else{
    chain.doFilter(req, resp);
    return;

if (isAJAXRequest(request)) {

        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>
            <partial-response><redirect url=\"").append(redirectURLAjax)
                .append("\"></redirect></partial-response>");
        response.setHeader("Cache-Control", "no-cache");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/xml");

        PrintWriter pw = response.getWriter();
        pw.println(sb.toString());
        pw.flush();

    } else { 
        response.sendRedirect(redirectURLRegular);
    }

如果会话为空重定向 - 常规和 AJAX 都会发生。在下一页(login.xhtml,requestScoped)中,我可以通过

@ManagedProperty("#{param.param1}")
private String param1;

如果我添加第二个参数"../login.xhtml?param1=noSession&param2=val2"- 常规请求工作(重定向发生并查看两个参数)但 AJAX 请求不起作用(没有重定向,没有任何反应)。这是萤火虫报告:

XML Parsing Error: not well-formed Location: moz-nullprincipal:{4584d37a-e799-43db-8379-b0451edca95c} Line Number 1, Column 120:

..."/admin/faces/login.xhtml?param1=noSession&param2=val2"></redirect></partial-r...
...-------------------------------------------------^

这是如何引起的,我们如何在过滤器中为 AJAX 调用设置多个参数?

4

1 回答 1

3

The & is a special character in XML representing the start of an entity like &amp;, &lt;, etc. The XML parser is implicitly looking for the name (amp, lt, etc) and the ending ;. However, you wasn't using it as such and hence the webbrowser's XML parser felt over it when it unexpectedly encountered an =, making it non-well-formed XML.

You need to escape the XML special character & into the entity &amp;.

redirectURLAjax = response.encodeRedirectURL(request.getContextPath()
            + "/faces/login.xhtml?param1=noSession&amp;param2=val2");
于 2013-11-16T10:07:34.243 回答