gpt4 book ai didi

json - 在单个请求中渲染多个 View

转载 作者:行者123 更新时间:2023-12-03 03:38:35 25 4
gpt4 key购买 nike

我尝试在单个请求中返回多个 View ,并以 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)
}

我正在使用 Spring MVC 和 Tiles 2。

有人可以帮助我吗?

更新 1 - 使用 ViewResolver 解析 View 名称:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/tiles/" />
<property name="suffix" value=".jsp" />
</bean>

更新 2 - 我创建了 github repository包含一个重现问题的非常小的示例。

最佳答案

更新:有一些方法可以解决您的问题。我会尝试这个,但也许还有更干净的方法。

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

关于json - 在单个请求中渲染多个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9705293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com