先上问题代码
@Controller
@RequestMapping("/guarantee")
public class GuaranteeController {
@Autowired
private NoticeService noticeService;
@RequestMapping({"", "/index"})
public ModelAndView index(ModelAndView mav){
mav.addObject("notices", noticeService.queryList(new NoticeQuery(Constants.NoticeType.GUARANTEE_NOTICE, 100)));
mav.setViewName("/guarantee/index");
return mav;
}
@RequestMapping("/notices")
@ResponseBody
public ModelMap notices(ModelMap map, NoticeQuery query){
query.setType(Constants.NoticeType.GUARANTEE_NOTICE);
List<Notice> notices = noticeService.queryList(query);
map.addAttribute("listData", notices)
.addAttribute("total", new PageInfo<>(notices).getTotal())
.addAttribute("query", query);
return map;
}
}
第一个方法是能够正常跳转页面的,但是页面上需要ajax调用接口请求数据,问题就来了。第二个方法虽然加上了@ResponseBody注解,但是请求一直报错找不到模板。折腾了一下午,百度不得其解,最后突然发现,这特么不跟我第一篇博客的问题一样吗???真的要被自己蠢哭了。。。
修改第二个方法,代码如下:
@RequestMapping("/notices")
@ResponseBody
public ModelMap notices(NoticeQuery query){
ModelMap map = new ModelMap();
query.setType(Constants.NoticeType.GUARANTEE_NOTICE);
List<Notice> notices = noticeService.queryList(query);
map.addAttribute("listData", notices)
.addAttribute("total", new PageInfo<>(notices).getTotal())
.addAttribute("query", query);
return map;
}
还是把 ModelMap 参数挪到方法里new出来就好了。。。以后一定得养成良好的习惯,ModelMap 要放到方法中new出来。