我使用的是Spring MVC。 我想通过转发它来渲染一些jsp。然后我想把结果写成json。
例如,我想渲染我复杂的jsp,在退出时我想得到: { “结果”: “OK”, “HTML”: “......。”}
我该怎么做? 我试过看 request.getRequestDispatcher(“tutorMini”)。forward(request,response) 但如果我无法通过响应,bcz它应该将所有输出写入它。
我尝试在jsp中使用一些json标签,但它在层次结构方面存在一些问题: HTML output with jsp:include and json-taglib
答案 0 :(得分:0)
由于在将HTML插入JSON(转义'
和"
)时需要应用其他转换,因此无法直接将JSP的输出写入响应。
因此,您需要创建一个ServletResponseWrapper
实例来保存输出(通过覆盖getWriter()
和/或getOutputStream()
)并将其传递给RequestDispatcher.include()
(它在这种情况下看起来比forward()
看起来更合适:
MyServletResponseWrapper wrapper = new MyServletResponseWrapper(response);
request.getRequestDispatcher("tutorMini").include(request, wrapper);
String html = wrapper.getSavedOutput();
然后,您可以将保存的内容插入JSON,并适当地转义。