Dima
Dima

Reputation: 1065

JAVA SPRING MVC Modify output stream before showing on the page

is there a proper way how I can modify output content right before Spring flushes it to the page? When content is ready I need to run a regex to fix all the links at ones.

Lets say I use interceptors, how do I get the rendered content, modify it and set it back ?

public class SpringControllerInterceptor extends HandlerInterceptorAdapter {

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");

    }
}

Upvotes: 1

Views: 2789

Answers (2)

Viral Patel
Viral Patel

Reputation: 8601

Spring MVC Interceptor would be the one of the way of doing this. Override postHandle method to update the http response object.

void postHandle(HttpServletRequest request,
                HttpServletResponse response,
                Object handler,
                ModelAndView modelAndView)
                throws Exception

Intercept the execution of a handler. Called after HandlerAdapter actually invoked the handler, but before the DispatcherServlet renders the view.

Read more:

  1. Spring MVC Interceptor example
  2. Spring Interceptor Documentation

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240948

You can add a Filter layer to process your response, You can grab whole response body and process the links you want

Upvotes: 1

Related Questions