Search code examples
javaspringspring-mvc

How to return rendered jsp view (html) as json field?


I want to do async pagination, i.e. browser makes Ajax call to Spring controller that would return json consisting of control data for pagination (current page, number of total pages) and field containing html with content. Is it possible to render html from jsp view and put it in json response as one of the fields?


Solution

  • Take a look at MVC flow:

    enter image description here

    As you see, the JSP generation will be done by View Template and the ready HTML will be returned to the browser by Front Controller (Dispatcher Servlet in Spring).

    That is why it is not possible to add Html as a part of the JSON response.

    Actually JSP is not the best solution for SPA applications and async page reloading.