I would like to write a Spring MVC HandlerInterceptorAdaptor which does different things in the postHandle() method based on wether the HttpResponse is a redirect or not.
Is this possible, and if so how?
public class MenuInterceptor extends HandlerInterceptorAdapter {
public final void postHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler,
ModelAndView modelAndView) throws SystemException {
if (redirect) {
// do somethnig
开发者_C百科} else {
// do something else
}
}
EDIT: Is there a better way than this:
if (modelAndView.getView() instanceof RedirectView || modelAndView.getViewName().startsWith("redirect:")) {
// Do something
} else {
// Do something else
}
In Spring MVC, a controller typically sends a redirect by returning a View
that is a RedirectView
or a String viewName that starts with the redirect:
prefix. You can easily check for either of these.
Exactly what kind of "interceptor" do you mean, and/or what kind of "HttpResponse"? If your HttpResponse has a "status code" or "response code", you just need to check that for a redirect code. Usually, 301 is used for redirects.
精彩评论