Reputation: 8412
I'm trying to implement a 301 redirect from within JSF but when I firebug it I always see a 302 being executed. Would someone be able to show me how to alter the method below to accomplish a 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);
}
}
Upvotes: 4
Views: 1773
Reputation: 1108722
You shouldn't invoke externalContext.redirect(destination)
. It will override the status to 302. You should manually set the Location
header and complete the response.
externalContext.setResponseStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
externalContext.setResponseHeader("Location", destination);
facesContext.responseComplete();
Upvotes: 5