IntelliJ IDEA在使用SpringBoot集成thymeleaf时,将html页面放在webapp/WEB-INF/view下报错无法访问
eclipse可以正常访问,但是IntelliJ IDEA下不可以,通过controller访问页面是直接报如下错误
org.thymeleaf.exceptions.TemplateInputException:
Error resolving template "login", template might not exist or might not be accessible by any of the configured Template Resolvers
解决方法:
在项目pom文件内,build标签下添加如下resource配置
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
<!--注意此次必须要放在此目录下才能被访问到 -->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
配置文件prefix指定路径
spring.thymeleaf.prefix=/WEB-INF/view/
spring.thymeleaf.mode=HTML5
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.cache=false
spring.thymeleaf.check-template=true
controller内返回页面名时前面不要带 /
@GetMapping(value = {"/login"})
public String login(Model model) {
model.addAttribute("host", "127.0.0.1");
return "login";
}