我想知道是否可以通过编程方式而不是通过控制器的返回参数生成 ModelAndView 的输出。例如:我有以下方法返回已编译的 html:
@RequestMapping(value = "/get-list", method = RequestMethod.GET, headers = BaseController.AJAX_HEADER)
public ModelAndView getList(@RequestParam(value = "page", required = true) Integer page,
@ActiveUser User activeUser) {
ModelAndView result = null;
try {
result = new ModelAndView("administration/events-log/list");
result.addObject("events", eventsLogService.getList(page, Config.RECORDS_PER_PAGE));
}
catch (Exception e) {
log(e, activeUser.getUsername());
}
return result;
}
我想要的是创建这样的东西:
@RequestMapping(value = "/get-list", method = RequestMethod.GET, headers = BaseController.AJAX_HEADER)
public @ResponseBody HashMap<String, Object> getList(@RequestParam(value = "page", required = true) Integer page,
@ActiveUser User activeUser) {
HashMap<String, Object> json = new HashMap<String, Object>();
try {
json.put("error", 0);
ModelAndView result = new ModelAndView("administration/events-log/list");
result.addObject("events", eventsLogService.getList(page, Config.RECORDS_PER_PAGE));
json.put("content", result);
}
catch (Exception e) {
/**/
}
return json;
}
所以将被发送回客户端的 JSON 对象看起来是:{'error': 0, 'content':compiled_html}
有什么想法吗?谢谢