3

Isn't there any easy way in Spring 4 by which we can achieve same as following code

request.getRequestDispatcher("/WEB-INF/jsp/account_summary.jsp").include(request, response);

To get output of a JSP file as a String in our controller. Presently I am using following code for this purpose

HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response) {
    private final StringWriter sw = new StringWriter();

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

    @Override
    public String toString() {
       return sw.toString();
    }
};
request.getRequestDispatcher("/WEB-INF/jsp/account_summary.jsp").include(request, responseWrapper);

I need output of JSP in a String so I can build a JSON object to return back. But I am not able to find a spring only solution to achieve this.

3
  • You want to return the HTML inside JSON for updating the DOM from an AJAX call? Commented Mar 17, 2014 at 16:38
  • @SotiriosDelimanolis Yes, not just html, there are some other fields as well that I am passing with JSON Commented Mar 17, 2014 at 17:03
  • The way you have seems fine. For much better results (IMO), use a templating library like Thymeleaf or Velocity. Commented Mar 17, 2014 at 18:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.