简体   繁体   中英

How can i get the HTML as string from HTTPServletResponse object In Spring?

I wanted to get the HTML Content of processed JSP from controller. I am using Tiles Implementation for Views.

In my scenario, I want to generate the HTML from jsp and send it as JSONP.

So i need to get hold of Generated html from controller.

Here you go!

This worked perfect for me:

@Service
public class CustomViewProcessor
{
    private static Logger m_log = LoggerFactory.getLogger(CustomViewProcessor.class);

    @Autowired
    @Qualifier("tilesViewResolver")
    private ViewResolver viewResolver;

    @Autowired
    @Qualifier("commonInterceptor")
    CommonInterceptor commonInterceptor;

    public String renderView(HttpServletRequest request, Model model, String viewName)
    {
        ModelAndView mav = new ModelAndView();
        mav.addAllObjects(model.asMap());
        mav.setViewName(viewName);
        commonInterceptor.updateRequest(mav, request);
        try
        {
            View view = viewResolver.resolveViewName(mav.getViewName(), request.getLocale());
            HttpServletResponse localResponse = new MyHttpServletResponseWrapper(new MockHttpServletResponse());

            view.render(mav.getModel(), request, localResponse);

            return localResponse.toString();
        }
        catch (Exception e)
        {
            return "";
        }
    }

    public boolean doesViewExist(HttpServletRequest request, String viewName)
    {
        try
        {
            if (viewResolver.resolveViewName(viewName, request.getLocale()) != null)
            {
                return true;
            }
        }
        catch (Exception e)
        {
            m_log.error(e.getMessage(), e);
        }

        return false;
    }

    static class MyHttpServletResponseWrapper extends HttpServletResponseWrapper
    {
        private StringWriter sw = new StringWriter();

        public MyHttpServletResponseWrapper(HttpServletResponse response)
        {
            super(response);
        }

        public PrintWriter getWriter() throws IOException
        {
            return new PrintWriter(sw);
        }

        public ServletOutputStream getOutputStream() throws IOException
        {
            throw new UnsupportedOperationException();
        }

        public String toString()
        {
            return sw.toString();
        }
    }
}

You need to annotate your controller method with @ResponseBody. Please look at the documentation for more details

Control flow leaves the DispatcherServlet and moves to the Servlet Container (eg Tomcat) before the JSP page template is populated. So Spring MVC will never have visibility into the final HTML generated from the JSP template.

If you want to pipe your final HTML output into a JSONP request, you're going to do have to implement that as a filter in your Servlet Container, which may or may not support that behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM