接口添加@ResponseBody注解后,thymeleaf依然报错:template might not exist or might not be accessible by any of the

2 篇文章 0 订阅
2 篇文章 0 订阅

先上问题代码

@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出来。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值