【发布时间】:2012-03-14 15:50:16
【问题描述】:
我正在尝试在单个请求中返回多个视图,并将它们全部返回到 JSON 字符串中。
例子:
@RequestMapping(value = "my-request")
public void myRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
{
Map<String,Object> model1 = new Hashtable<String,Object>();
model1.put(...);
ModelAndView modelAndView1 = new ModelAndView("path/to/view1", model1);
// Render modelAndView1 in some way, in order to obtain the rendered HTML as a String
Map<String,Object> model2 = new Hashtable<String,Object>();
model2.put(...);
ModelAndView modelAndView2 = new ModelAndView("path/to/view2", model2);
// Render modelAndView2 in some way, in order to obtain the rendered HTML as a String
// Now write a JSON String to the HttpServletResponse containing both the rendered views (strings).
// (this is not part of my problem, I'm able to do it as long as I have the two strings)
}
我正在使用带有 Tiles 2 的 Spring MVC。
谁能帮帮我?
更新 1 - 使用 ViewResolver 解析视图名称:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/tiles/" />
<property name="suffix" value=".jsp" />
</bean>
更新 2 - 我创建了一个 github repository,其中包含一个重现问题的非常小的示例。
【问题讨论】:
-
所以实际上您不想显示任何视图,而是将字符串传递给您的客户?
-
我所说的字符串是呈现为字符串的 HTML(相应地更改了原始问题)
-
如果您使用磁贴将视图平铺成单个复合视图并重定向到它的名称。
-
我不想使用单个复合视图,我需要视图保持独立。 JSON 响应将由 JavaScript 管理,该 JavaScript 将在此处放置一个视图并在此处放置一个视图
标签: json spring spring-mvc tiles