2016-02-05 70 views
0

在我的Spring MVC项目中,我添加了一个拦截器类来检查重定向是否被触发。如何签入拦截器是否触发重定向

这里是我的控制器类:

@Controller 
public class RedirectTesterController { 

    @RequestMapping (value="/page1") 
    public String showPage1(){ 

     return "page1"; 
    } 

    @RequestMapping (value="/submit1") 
    public String submitPage1(){ 

     return "redirect:/page2"; 
    } 

    @RequestMapping (value="/page2") 
    public String showPage2(){ 

     return "page2"; 
    } 

} 

所以,如果我打电话例如

本地主机:8080/MyContext/submit1

执行方法 “submitPage1”。

现在 - 服务器告诉客户端,调用

本地主机:8080/MyContext/2页

这也是工作。

所以 - 我想在执行方法“submitPage1”之后进入该过程。 在我看来,httpResponse中应该有一些命令/命令,我可以问。

为了检查,我在方法中的拦截器类中做了一个断点:“postHandle” - 从那时起,我不知道如何继续。 我试图读取outputStream - 但这样做会崩溃我的应用程序。 (导致一个异常 - > outputStream已被调用..)。

这难道不是一个简单的解决方案吗?

回答

0

下面的示例演示如何测试一个视图是一个redirect

@Configuration 
public class MvcConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void addInterceptors(InterceptorRegistry registry) { 
     registry.addInterceptor(new HandlerInterceptorAdapter() { 
      @Override 
      public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, 
            ModelAndView modelAndView) throws Exception { 
       if (modelAndView != null && StringUtils.startsWithIgnoreCase(modelAndView.getViewName(), "redirect:")) { 
        // handle redirect... 
       } 
      } 
     }); 
    } 
} 

见:HandlerInterceptorAdapterStringUtils

Spring MVC的文档:Intercepting requests with a HandlerInterceptor