部分响应xml显示在浏览器上而不是页面本身

时间:2015-05-25 07:50:59

标签: xml spring jsf primefaces partial-response

我目前正在使用 Spring Primefaces 4.0 开发JSF应用程序。在重定向到其他页面时,我们有关于显示XML页面而不是页面本身的间歇性问题。

这似乎与此https://stackoverflow.com/questions/29178778/partial-response-view-expired-exception-or-malformed-xhtml相似。此外,只要发生这种情况,日志中就不会打印错误。

PartialResponse 1:

<partial-response>
    <error>
          <error-name>class java.lang.IndexOutOfBoundsException</error-name>
          <error-message>
              <![CDATA[ Index: 0, Size: 0 ]]>
          <error-message>
    </error>
</partial-response>

PartialResponse 2:

<partial-response>
   <changes>
       <update id="javax.faces.ViewState">-4640542341950585998:4269573232206322924</update>
   </changes>
</partial-response>

我们目前正在使用Omnifaces 2.0 FullAjaxExceptionHandlerFactory ,我们还在会话管理过滤器中使用了banterCz的 JSFRedirectStrategy 来过期会话重定向。

JsfRedirectStrategy.java

public class JsfRedirectStrategy implements InvalidSessionStrategy {

private Logger logger = LoggerFactory.getLogger(getClass());

private static final String FACES_REQUEST_HEADER = "faces-request";

private String invalidSessionUrl;

/**
 * {@inheritDoc}
 */
@Override
public void onInvalidSessionDetected(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {        

    boolean ajaxRedirect = "partial/ajax".equals(request.getHeader(FACES_REQUEST_HEADER));
    if(ajaxRedirect) {
        String contextPath = request.getContextPath();
        String redirectUrl = contextPath + invalidSessionUrl;
        logger.debug("Session expired due to ajax request, redirecting to '{}'", redirectUrl);

        String ajaxRedirectXml = createAjaxRedirectXml(redirectUrl);
        logger.debug("Ajax partial response to redirect: {}", ajaxRedirectXml);

        response.setContentType("text/xml");
        response.getWriter().write(ajaxRedirectXml);
    } else {
        String requestURI = getRequestUrl(request);
        logger.debug("Session expired due to non-ajax request, starting a new session and redirect to requested url '{}'", requestURI);
        request.getSession(true);
        response.sendRedirect(requestURI);
    }

}

private String getRequestUrl(HttpServletRequest request) {
    StringBuffer requestURL = request.getRequestURL();

    String queryString = request.getQueryString();
    if (StringUtils.hasText(queryString)) {
        requestURL.append("?").append(queryString);
    }

    return requestURL.toString();
}

private String createAjaxRedirectXml(String redirectUrl) {
    return new StringBuilder()
                    .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
                    .append("<partial-response><redirect url=\"")
                    .append(redirectUrl)
                    .append("\"></redirect></partial-response>")
                    .toString();
}

public void setInvalidSessionUrl(String invalidSessionUrl) {
    this.invalidSessionUrl = invalidSessionUrl;
}

}

面-config.xml中

<factory>
     <exception-handler-factory>org.omnifaces.exceptionhandler.FullAjaxExceptionHandlerFactory</exception-handler-factory>
</factory>

的web.xml

<context-param>
    <param-name>javax.faces.FACELETS_BUFFER_SIZE</param-name>
    <param-value>1048576</param-value>
</context-param>

<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/error/viewexpiredpage.xhtml</location>
</error-page>

<error-page>
    <error-code>404</error-code>
    <location>/error/pagenotfound.xhtml</location>
</error-page>

<error-page>
    <error-code>500</error-code>
    <location>/error/globalerrorpage.xhtml</location>
</error-page>

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/error/globalerrorpage.xhtml</location>
</error-page>

<error-page>
    <exception-type>org.springframework.web.client.RestClientException</exception-type>
    <location>/error/globalerrorpage.xhtml</location>
</error-page>

<error-page>
    <exception-type>org.springframework.web.client.HttpServerErrorException</exception-type>
    <location>/error/globalerrorpage.xhtml</location>
</error-page>

0 个答案:

没有答案