IntelliJ IDEA使用SpringBoot集成thymeleaf时,页面放在WEB-INF下报错无法访问

IntelliJ IDEA在使用SpringBoot集成thymeleaf时,将html页面放在webapp/WEB-INF下报错无法访问
eclipse可以正常访问,但是IntelliJ IDEA下不可以,通过controller访问页面是直接报如下错误
org.thymeleaf.exceptions.TemplateInputException:
Error resolving template "page", template might not exist or might not be accessible by any of the configured Template Resolvers

org.thymeleaf.exceptions.TemplateInputException: 
Error resolving template "page", 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>

该段配置意思是编译的时候把webapp文件放到resource下
然后检查下其它配置是否写错:
1:配置文件prefix指定路径

#关闭thymeleaf缓存
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
#在构建URL时预先查看名称的前缀,默认:resources/templates/
spring.thymeleaf.prefix=/WEB-INF/
#构建URL时附加查看名称的后缀
spring.thymeleaf.suffix=.html

2:controller内返回页面名时前面不要带 /

@RequestMapping("/page")
public String toTest() {
    return "page";
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。