我从Controller获取外部网页(作为字符串)。如何将包含HTML的字符串作为ModelAndView
?
答案 0 :(得分:4)
如果您想将该网页返回到客户端/浏览器,则无需创建ModelAndView。
而是这样做
@Controller
public class MyController() {
@RequestMapping(...)
@ResponseBody
public String controllerMethod() {
String htmlDocument = getHtmlFromSomeWhere();
return htmlDocument;
}
}
如果您必须返回ModelAndView
,则必须编写自己的View
实施。
查看某些模板的AbstractPdfView
。
return ModelAndView(new MyPlainHtmlView(htmlDocument));
public class MyPlainHtmlView implements View {
....
private final String htmlDocument;
public MyPlainHtmlView(String htmlDocument) {
this.htmlDocument = htmlDocument;
}
@Override
renderMergedOutputModelMap<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
ServletOutputStream out = response.getOutputStream();
out.write(this.htmlDocument.getBytes("utf-8"));
}
}