JSF 2 - 永久重定向(301)

时间:2012-05-24 20:39:12

标签: jsf redirect jsf-2 http-status-code-301

我正在尝试从JSF中实现301重定向,但是当我实施它时,我总是看到302被执行。有人能告诉我如何改变下面的方法来完成301?

/**
 * Redirect to specified destination url
 *
 * @param destination
 */
public static void permanentRedirect(String destination) {
    final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();

    try {
        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);

        if (!response.isCommitted()) {
            externalContext.redirect(destination);
        }
    } catch (IOException e) {
        LOGGER.debug("Could not redirect to " + destination);
    }
} 

1 个答案:

答案 0 :(得分:4)

您不应该调用externalContext.redirect(destination)。它将覆盖状态为302.您应该手动set the Location headercomplete the response

externalContext.setResponseStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
externalContext.setResponseHeader("Location", destination);
facesContext.responseComplete();