HandlerInterceptorAdapter 是 Spring MVC 中的拦截器,它可以用来拦截 Controller 的请求和返回值。

在拦截 Controller 返回值时,可以通过实现 HandlerInterceptorAdapter 的 postHandle 方法,在 Controller 处理完请求后,对返回值进行处理。

例如,可以在 postHandle 方法中获取 Controller 返回的 ModelAndView 对象,然后对其进行修改,或者添加一些额外的数据。

下面是一个简单的示例:

public class MyInterceptor extends HandlerInterceptorAdapter {

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        if (modelAndView != null) {
            modelAndView.addObject("extraData", "some extra data");
        }
    }
}

在上面的代码中,我们实现了 HandlerInterceptorAdapter 的 postHandle 方法,如果 Controller 返回了 ModelAndView 对象,我们就在其中添加了一个名为 "extraData" 的属性。

这样,在 Controller 中返回的 ModelAndView 对象中,就会包含我们添加的额外数据

HandlerInterceptorAdapter 拦截Controller返回值

原文地址: https://www.cveoy.top/t/topic/fgAs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录