开发者

Is there a way to check if the HttpResponse is a redirect or not in a Spring interceptor?

开发者 https://www.devze.com 2023-04-11 09:18 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消