Dav*_*ple 4 jsf redirect http-status-code-301 jsf-2
我试图从 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);
}
}
Run Code Online (Sandbox Code Playgroud)
你不应该调用externalContext.redirect(destination). 它将把状态覆盖为 302。您应该手动设置Location标头并完成响应。
externalContext.setResponseStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
externalContext.setResponseHeader("Location", destination);
facesContext.responseComplete();
Run Code Online (Sandbox Code Playgroud)