springboot + thymeleaf静态资源访问404
在使用springboot 和thtmeleaf开发时引用静态资源404,静态资源结如下:

index.html文件:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset = "UFT-8" />
<title>Spring Boot Application</title>
<link href="css/style.css" rel="stylesheet">
<!--<link th:href="@{css/style.css}" rel="stylesheet">-->
</head>
<body>
<h4>Welcome to Thymeleaf Spring Boot web application 乱码乱码</h4>
<p th:text="${hello}">hello</p>
<p th:text="${hi}">hi</p>
<input type="text" />
</body>
</html>
style.css文件
h4 {
color: red;
}
p {
color: blue;
}
测试访问url
@Controller
@RequestMapping(value = "thymeleaf")
public class IndexController { @RequestMapping(value = "index")
public String index(Model model, ModelMap modelMap) { model.addAttribute("hello", "thymeleaf"); modelMap.addAttribute("hi", "thymeleaf"); return "index";
} @RequestMapping(value = "hello")
public String hello(ModelMap modelMap) { modelMap.put("hei", "thymeleaf"); return "hello";
}
}
配置文件application
#thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.mode=HTML
启动项目后访问http://127.0.0.1:8080/thymeleaf/index,写入的样式并未引进项目中,打开控制台发现静态资源访问的url上加上了后台请求url的除去最后以为的字符串(如果是/thymeleaf/index/hello将会是http://127.0.0.1:8080/thymeleaf/index/css/style.css),显然这并不是静态资源访问位置,404也就正常了。

而直接访问http://127.0.0.1:8080/css/style.css是ok的。

这个问题就是英文引入静态资源文件的路径写的不对,index.html中的引入应该写成
<link href="/css/style.css" rel="stylesheet">
<!--<link th:href="@{/css/style.css}" rel="stylesheet">-->
加上“/”表示绝对路径
springboot + thymeleaf静态资源访问404的更多相关文章
- IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404
IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404 .embody{ padding:10px 10px 10px; margin:0 -20px; borde ...
- springboot配置静态资源访问路径
其实在springboot中静态资源的映射文件是在resources目录下的static文件夹,springboot推荐我们将静态资源放在static文件夹下,因为默认配置就是classpath:/s ...
- 关于SpringBoot中静态资源访问的问题
第一种方式 : 放在src/main/webapp目录下 第二种方式:放在classpath下(网页存放于static目录下, 默认的"/"指向的是~/resouces/stati ...
- springboot - 登录+静态资源访问+国际化
1.项目目录结构 2.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmln ...
- SpringBoot静态资源访问+拦截器+Thymeleaf模板引擎实现简单登陆
在此记录一下这十几天的学习情况,卡在模板引擎这里已经是四天了. 对Springboot的配置有一个比较深刻的认识,在此和大家分享一下初学者入门Spring Boot的注意事项,如果是初学SpringB ...
- 聊聊、SpringBoot 静态资源访问
SpringBoot 1.X 版本和 SpringBoot 2.X 版本在静态资源访问上有一些区别,如果直接从 1.X 升级到 2.X 肯定是有问题的.这篇文章就来讲讲这方面问题,也是项目中的坑. 先 ...
- SpringBoot 常用配置 静态资源访问配置/内置tomcat虚拟文件映射路径
Springboot 再模板引擎中引入Js等文件,出现服务器拒绝访问的错误,需要配置过滤器 静态资源访问配置 @Configuration @EnableWebMvc public class Sta ...
- springboot学习入门简易版四---springboot2.0静态资源访问及整合freemarker视图层
2.4.4 SpringBoot静态资源访问(9) Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则 /static /public /resourc ...
- springboot 静态资源访问,和文件上传 ,以及路径问题
springboot 静态资源访问: 这是springboot 默认的静态资源访问路径 访问顺序依次从前到后(http://localhost:8080/bb.jpg) spring.resourc ...
随机推荐
- 《GO Home Trash!》UML类图,ER图以及数据库设计
<Go Home Trash!>UML类图 ER图以及数据库中数据表 分析: 这款软件经过我们前期的讨论以及需求分析,确定了用户,客服以及管理员三个实体.在设计UML类图时,对各个实体之间 ...
- js多重数组完全展开
有时候项目中会遇到多重数组,需要判断多重数组里面有没有要找的对象,强大的js就可以帮助我们 var arrTest = [1, [2, 3, [4]], 5, 6, [7, 8], [[9, [10, ...
- 使用SpringSecurity保护程序安全
首先,引入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- codeforces 1041 E. Tree Reconstruction 和度数有关的构造树
CF 1041E:http://codeforces.com/contest/1041/problem/E 题意: 告诉你一个树的节点个数,显然有n-1条边.已知去掉一条边后,两个集合中最大的节点值. ...
- lightoj 1119 - Pimp My Ride(状压dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1119 题解:状压dp存一下车有没有被搞过的状态就行. #include < ...
- go Server示例
示例1: package main import ( "fmt" "log" "net/http" "time" ) f ...
- Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.apache.catalina.connector.CoyoteWriter and no properties discovered to create BeanSerializer
一.什么是序列化In computer science, in the context of data storage, serialization is the process of transla ...
- 【LeetCode】17-电话号码的字母组合
题目描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出: ...
- SpringBoot系列__01HelloWorld
接触SpringBoot很久了,但是一直没有很深入的研究一下源码,最近重启了博客,顺便开始深入研究一下技术. 1.简介 参照官方文档的说法,SpringBoot的设计理念就是为了简化Java程序员搭建 ...
- springboot使用 @EnableScheduling、@Scheduled开启定时任务
1.在main启动项添加一个注解@EnableScheduling package com.example.springmybatis; import org.mybatis.spring.annot ...