Get jsp output in spring controller as String

我与影子孤独终老i 提交于 2020-01-03 21:08:25

问题


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.

来源:https://stackoverflow.com/questions/22457673/get-jsp-output-in-spring-controller-as-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!