Spring MVC - AJAX-JSON Response to contain rendered JSP view

后端 未结 1 428
南方客
南方客 2021-01-07 08:35

I need my controller to return an AJAX JSON response that contains the updated HTML code.

The updated HTML code is created by rendering a JSP view.

For examp

相关标签:
1条回答
  • 2021-01-07 09:01

    If the view you want to capture is in /WEB-INF/views/my.jsp, then call

    request.getRequestDispatcher("/WEB-INF/views/my.jsp").include(request, myResponse);    
    

    where myResponse is either a HttpServletResponseWrapper that you've created, or a Spring MockHttpServletResponse. In the latter case you can get the rendered output from getContentAsString().

    EDIT below

    I ran into another SO question around capturing servlet responses that had some pointers to HttpServletResponseWrappers that you can use.

    Two implementations that look good:

    1. DWR SwallowingHttpServletResponse
    2. Sitemesh PageResponseWrapper

    Enjoy,

    0 讨论(0)
提交回复
热议问题