【问题标题】:JSF 2 -- Permanent Redirect (301)JSF 2 -- 永久重定向 (301)
【发布时间】:2012-05-31 11:27:28
【问题描述】:

我正在尝试从 JSF 中实现 301 重定向,但是当我对其进行 firebug 时,我总是看到正在执行 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);
    }
} 

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 啊哈!再次感谢。老实说,我不知道没有你 Java/JSF 会做什么。我希望甲骨文在圣诞节给你寄一张不错的大支票。
    • 不客气。我希望你收到了ExternalContext#setResponseStatus() 的提示:)
    • @BalusC 我正在使用这种重定向是 301 吗? HttpServletResponse res = (HttpServletResponse) 响应; res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); res.setHeader("位置", newUrl); res.flushBuffer();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 2013-07-17
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    相关资源
    最近更新 更多