当前位置: 首页 > news >正文

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

目录

  • 一、异常错误
  • 二、原因
  • 三、解决方法
    • 方法1. 将无法编译的静态资源放入可编译目录下
    • 方法2. 重新编译项目加载资源
    • 方法3. 修改pom.xml资源配置文件
    • 方法4. 不连接远程数据库启动,使用本地数据库

一、异常错误

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]
http://www.lryc.cn/news/18581.html

相关文章:

  • 服务端IOS订阅类型支付接入详细说明与注意事项
  • 【剑指Offer】重建二叉树(递归+迭代)
  • 注解@Transactional 原理和常见的坑
  • 2023年全国最新交安安全员精选真题及答案4
  • 扬帆优配|半天翻倍,“蹭热点”翻车,前期“牛股”已近腰斩
  • 6 种易于上手的编程副业,每月赚取 1,000 多美元——没有废话
  • 第九届蓝桥杯省赛 C++ B组 - 日志统计
  • 记一次服务器入侵事件的应急响应
  • 作用域链查找机制(回顾)
  • 前端基础之HTML扫盲
  • 大规模食品图像识别:T-PAMI 2023论文解读
  • 【java】Spring Cloud --Spring Cloud Alibaba RocketMq 异步通信实现
  • 玫瑰花变蚊子血,自动化无痕浏览器对比测试,新贵PlayWright Vs 老牌Selenium,基于Python3.10
  • Spring Cloud入门篇 Hello World | Spring Cloud 1
  • C++学习笔记-数据结构
  • 【C++的OpenCV】第五课-OpenCV图像常用操作(二):OpenCV的基本绘图、平滑滤波(模糊)处理
  • [SSD固态硬盘技术 19] 谁是数据的守护神? 盘内RAID1/RAID5图文详解_盘内数据冗余保护
  • linux相对于windows环境为啥相对来说更加具有安全性
  • iOS开发笔记之九十七——关于Restful API的一些总结
  • Linux系统Nginx下载和安装
  • 交叉编译 acl
  • wait/notify方法 等待唤醒机制
  • c++笔记之构造函数中的default作用
  • 【代码随想录二刷】Day24-回溯-C++
  • Kubernetes中YAML 文件简介
  • 骨传导耳机是怎么发声的,骨传导耳机值得入手嘛
  • 会声会影2023官方新功能介绍
  • vue:pdf.js使用细节/隐藏按钮/设置、获取当前页码/记录阅读进度/切换语言(国际化)
  • RocketMQ实现延迟队列精确到秒级实现
  • 线性数据结构:数组 Array