Springboot 使用thymeleaf 服务器无法加载resources中的静态资源异常处理

简介: Springboot 使用thymeleaf 服务器无法加载resources中的静态资源异常处理

一、异常错误

Springboot使用thymeleaf,并连接远程数据库启动时,无法加载resources中的静态资源

浏览器报错

Failed to load resource: the server responded with a status of 404 ()

后端启动时报错

Servlet.service() for servlet [dispatcherServlet] in context with path [/ce] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template 

前端打开页面时后端报错

Exception processing template "/web/studyOutline/studyOutline": Error resolving template [/web/studyOutline/studyOutline], template might not exist or might not be accessible by any of the configured Template Resolvers

二、原因

打包编译项目,显示找不到js、css、html等静态资源,但本地路径并没有写错,于是我去找编译文件,查看是不是静态资源没有编译到,打开项目下的target文件夹

前往classes文件夹,发现项目resources下对应的templates韦文件夹没有编译到,缺少静态资源,当然会报错了

三、解决方法

方法1. 将无法编译的静态资源放入可编译目录下

既然服务器不能编译templates文件夹,那么把templates文件夹放入calsses路径下即可,这样处理就能获取templats下的静态资源了,但如果静态资源有改动,需要手动放入classes文件夹下,再次启动项目即可读取资源

如果再次启动项目,还是显示找不到js、css、html等静态资源,请看方法2

方法2. 重新编译项目加载资源

由于服务器编译拦截了静态资源,导致出现异常,需要重新打包编译

打开IDEA带的Maven管理,双击clean清除由项目编译创建的target

再双击install安装jar包到本地仓库

项目打包出现异常

[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.

系统默认编码是GBK,maven提升需要使用UTF-8,在setting中修改项目编码为UTF-8


出现 Failed to execute goal是由于测试用例有问题,

 Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-test) on project springboot_04_profile: Input length = 1 -> [Help 1]

选择跳过测试用例

再次双击install,编译成功,启动项目即可读取静态资源

如果设置编码还是打包失败,或者显示找不到js、css、html等静态资源,请看方法3

方法3. 修改pom.xml资源配置文件

如果设置编码还是打包失败,或者显示找不到js、css、html等静态资源,说明服务器没有访问资源的权限,需要在pom.xml的build下引入资源文件

<!--           引入静态资源文件   -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.css</include>
                    <include>**/*.js</include>
                    <include>**/*.html</include>
                    <include>**/*.png</include>
                    <include>**/*.properties</include>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                    <include>**/*.conf</include>
                </includes>
            </resource>
        </resources>

再次insall,显示打包成功,浏览器404的问题也解决了,加载了静态资源

方法4. 不连接远程数据库启动,使用本地数据库

开头说了,Springboot使用thymeleaf,并连接远程数据库启动时,无法加载resources中的静态资源,这是一个大坑,如果不连接远程数据库启动,则不存在服务器访问资源的问题

打开application.properties配置文件,注释掉连接远程数据库的代码,改用本地数据库,就不会有访问资源的问题了,可以直接加载,浏览器不再出现Failed to load resource问题

异常索引


Failed to load resource: the server responded with a status of 404 ()

Servlet.service() for servlet [dispatcherServlet] in context with path [/ce] threw exception [Request processing failed; nested

exception is org.thymeleaf.exceptions.TemplateInputException: Error

resolving template

Exception processing template “/web/studyOutline/studyOutline”: Error resolving template [/web/studyOutline/studyOutline], template

might not exist or might not be accessible by any of the configured

Template Resolvers

[INFO] Using ‘UTF-8’ encoding to copy filtered resources. [INFO] Using ‘UTF-8’ encoding to copy filtered properties files.

Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources

(default-test) on project springboot_04_profile: Input length = 1 ->

[Help 1]


目录
相关文章
|
18天前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
3月前
|
JavaScript 前端开发 Java
SpringBoot之静态资源规则与定制化
SpringBoot之静态资源规则与定制化
|
3月前
|
移动开发 Java HTML5
Springboot web静态资源配置
Springboot web静态资源配置
54 0
|
18天前
|
缓存 前端开发 Java
SpringBoot启动后加载初始化数据
SpringBoot启动后加载初始化数据
|
19天前
|
Java 测试技术 数据库
SpringBoot启动时设置不加载数据库
SpringBoot启动时设置不加载数据库
10 0
|
1月前
|
Java Spring
springboot项目读取 resources 目录下的文件的9种方式(总结)
springboot项目读取 resources 目录下的文件的9种方式(总结)
203 1
|
1月前
|
XML Java 数据格式
【springboot原理篇】Bean的加载方式,面试必看
【springboot原理篇】Bean的加载方式,面试必看
|
2月前
|
前端开发 Java API
构建异步高并发服务器:Netty与Spring Boot的完美结合
构建异步高并发服务器:Netty与Spring Boot的完美结合
|
3月前
|
Java Maven Docker
SpringBoot项目打包部署到阿里云服务器、通过Maven插件制作Docker镜像、部署项目容器、配置生产环境
SpringBoot项目打包部署到阿里云服务器、通过Maven插件制作Docker镜像、部署项目容器、配置生产环境
128 0
|
3月前
|
Java
springboot WebMvcConfigurer详解自定义配置请求静态资源
springboot WebMvcConfigurer详解自定义配置请求静态资源

热门文章

最新文章