在单个请求中呈现多个视图 [英] Render multiple views within a single request
本文介绍了在单个请求中呈现多个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在单个请求中返回多个视图,并将它们全部返回为JSON字符串.
I'm trying to return multiple views within a single request, returning them all in a JSON string.
示例:
@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)
}
我正在将Spring MVC与Tiles 2一起使用.
I'm using Spring MVC with Tiles 2.
有人可以帮助我吗?
更新1 -使用ViewResolver
解析视图名称:
Update 1 - View names are resolved using a ViewResolver
:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/tiles/" />
<property name="suffix" value=".jsp" />
</bean>
更新2 -我创建了一个 github存储库,其中包含一个很小的例子重现了问题.
Update 2 - I've created a github repository containing a very small example reproducing the problem.
推荐答案
更新:有一些方法可以解决您的问题.我会尝试这种方法,但也许有更清洁的方法.
UPDATE: There are some approaches for solving your issue. I'd try this one, but maybe there are cleaner ways.
@Component
public class JsonMultipleViewFactory {
@Autowired private List<ViewResolver> viewResolverList;
public View getView(List<ModelAndView> mavList) {
for (ModelAndView mav : mavList) {
if (mav.getView()==null) {
mav.setView(resolve(mav.getViewName()));
}
}
return new JsonMultipleView(mavList);
}
private View resolve(String viewName) {
for (ViewResolver vr : viewResolverList) {
View view = vr.resolve(viewName, LocaleContextHolder.getLocale());
if (view!=null) {
return view;
}
}
return null;
}
}
public class JsonMultipleView implements View {
private final List<ModelAndView> mavList;
public JsonMultipleView(List<ModelAndView> mavList) {
this.mavList = mavList;
}
public String getContentType() {
return "application/json";
}
public void render(Map<String,?> model, HttpServletRequest request, HttpServletResponse response) {
Json json = new Json(); // You can use several Json libraries here
for (ModelAndView mav : mavList) {
MockHttpServletResponse mockResponse = new MockHttpServletResponse();
mav.getView().render(mav.getModel(), request, mockResponse);
json.add(mav.getViewName(), mockResponse.getContentAsString());
}
json.write(response.getOutputStream());
response.getOutputStream().close();
}
}
并且可以像这样使用:
@Autowired private JsonMultipleViewFactory jsonMultipleViewFactory;
@RequestMapping(value = "my-request")
public View myRequest() {
...
List<ModelAndView> mavList = new ArrayList<ModelAndView>();
mavList.add(modelAndView1);
mavList.add(modelAndView2);
return jsonMultipleViewFactory.getView(mavList);
}
这篇关于在单个请求中呈现多个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文