4

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);
    }
} 
4

1 に答える 1

5

を呼び出すべきではありませんexternalContext.redirect(destination)。ステータスを 302に上書きします。ヘッダーを手動で設定Locationし、応答を完了する必要があります。

externalContext.setResponseStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
externalContext.setResponseHeader("Location", destination);
facesContext.responseComplete();
于 2012-05-24T20:47:05.300 に答える