异常:Error resolving template "users/list", template might not exist or might not be accessible by...

spring.MVC调试时异常:Error resolving template “users/list”, template might not exist or might not be accessible by any of the configured Template Resolvers.

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "users/list", template might not exist or might not be accessible by any of the configured Template Resolvers
	at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:865) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
	at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:608) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) [thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) [thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
	at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335) [thymeleaf-spring4-3.0.3.RELEASE.jar:3.0.3.RELEASE]
	at org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:189) [thymeleaf-spring4-3.0.3.RELEASE.jar:3.0.3.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1282) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]

  • 项目开发环境:Eclipse+SpringBoot+thymeleaf;

最近在学习spring boot开发系统,本地测试开发页面都能正常显示,但启动gradle项目之后,/users无法访问到,百度且尝试了很多方法,最终成功解决,把查询到的方法做一个整理,方便日后查看:

问题一:提示模板页不存在,但实际能找到模板页,但在使用th:include标签的时候才会出错。

  • 解决:引用模板文件时不用“/”打头,通过类似相对路径的方式来引用,但这里的相对路径仍然是相对于模板根目录来做的。
@RequestMapping("/view")
public String view()  {
    return "/view";
}

去掉“/”:

    return "view";

问题二:注解错误,在controller上加注解@Controller 和@RestController都可以在前端调通接口,但是二者的区别如下:

  • 当用@Controller时在方法上必须添加注解@ResponseBody,不添加则报错。因为当使用@Controller 注解时,spring默认方法返回的是view对象(页面),加上@ResponseBody,则方法返回的就是具体对象。

  • @RestController的作用就相当于@Controller+@ResponseBody的结合体(如下图)
    异常:Error resolving template

    问题三及解决:确定模板是否在默认templates文件夹里面,并且路径要和返回的View名字一致(类似如下图):

    异常:Error resolving template
    各位已经学成或正在学习新知识的都一起加油加油~冲冲冲!!

你可能感兴趣的:(spring,boot)