本文介绍了Spring MVC的 - AJAX,JSON响应包含渲染JSP视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的控制器返回一个AJAX JSON响应包含更新的HTML code。

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

更新HTML code是通过渲染一个JSP视图中创建。

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

例如:JSP:

<tr>
<td>${data1}</td>
<td>${data2}</td>
</tr>

JSON响应:

JSON response:

{"columns" : "2", "rows":"1", "data":rendered view}

目前我试图创建一个我自己的OutputStream一个虚拟的响应,并把在JSON响应渲染视图的内容,但没有运气。

Currently I'm trying to create a dummy response with "my own" outputstream and put the rendered view content in the json response, but with no luck.

除了事实,我不能让这个解决方案的工作,它不适合我。用正确的方法任何提示做呢?

Other than the fact I can't get this solution to work, it doesn't feel right.Any tips on the proper way to do it?

谢谢,大利

推荐答案

如果你想捕捉的看法是 /WEB-INF/views/my.jsp ,然后调用

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);

在这里myResponse或者是一个 HttpServletResponseWrapper 您已经创建,或弹簧 MockHttpServletResponse 。在后一种情况下,可以得到从getContentAsString()输出呈现

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().

下面

我遇到了另一个SO question各地捕获的servlet反应,有一些指向HttpServletResponseWrappers,你可以使用。

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

两个实现,好看:

  1. DWR SwallowingHttpServletResponse
  2. SiteMesh的 PageResponseWrapper
  1. DWR SwallowingHttpServletResponse
  2. Sitemesh PageResponseWrapper

享受,

这篇关于Spring MVC的 - AJAX,JSON响应包含渲染JSP视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:56