在使用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的更多相关文章

  1. IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404

    IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404 .embody{ padding:10px 10px 10px; margin:0 -20px; borde ...

  2. springboot配置静态资源访问路径

    其实在springboot中静态资源的映射文件是在resources目录下的static文件夹,springboot推荐我们将静态资源放在static文件夹下,因为默认配置就是classpath:/s ...

  3. 关于SpringBoot中静态资源访问的问题

    第一种方式 : 放在src/main/webapp目录下 第二种方式:放在classpath下(网页存放于static目录下, 默认的"/"指向的是~/resouces/stati ...

  4. springboot - 登录+静态资源访问+国际化

    1.项目目录结构 2.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmln ...

  5. SpringBoot静态资源访问+拦截器+Thymeleaf模板引擎实现简单登陆

    在此记录一下这十几天的学习情况,卡在模板引擎这里已经是四天了. 对Springboot的配置有一个比较深刻的认识,在此和大家分享一下初学者入门Spring Boot的注意事项,如果是初学SpringB ...

  6. 聊聊、SpringBoot 静态资源访问

    SpringBoot 1.X 版本和 SpringBoot 2.X 版本在静态资源访问上有一些区别,如果直接从 1.X 升级到 2.X 肯定是有问题的.这篇文章就来讲讲这方面问题,也是项目中的坑. 先 ...

  7. SpringBoot 常用配置 静态资源访问配置/内置tomcat虚拟文件映射路径

    Springboot 再模板引擎中引入Js等文件,出现服务器拒绝访问的错误,需要配置过滤器 静态资源访问配置 @Configuration @EnableWebMvc public class Sta ...

  8. springboot学习入门简易版四---springboot2.0静态资源访问及整合freemarker视图层

    2.4.4 SpringBoot静态资源访问(9) Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则 /static  /public  /resourc ...

  9. springboot 静态资源访问,和文件上传 ,以及路径问题

    springboot 静态资源访问: 这是springboot 默认的静态资源访问路径  访问顺序依次从前到后(http://localhost:8080/bb.jpg) spring.resourc ...

随机推荐

  1. CentOS重置MySQL root密码的方法

    1.修改MySQL的登录设置: # vim /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-tables 例如: [mysqld] skip-grant-tables  ...

  2. Spring依赖注入浅析

    1. 概念理解 依赖注入 谁注入谁? 依赖对象注入IoC容器. 控制反转 谁控制谁?控制什么? IoC容器控制对象,控制依赖对象的创建与注入. 为什么称为反转?创建.注入对象的控制权由程序员的主观意愿 ...

  3. .Net 基于Memcache集群的分布式Session

    简述 基于Memcache的Session大家都各有各的说法,比方说:当memcached集群发生故障(比如内存溢出)或者维护(比如升级.增加或减少服务器)时,用户会无法登录,或者被踢掉线等等,每种技 ...

  4. SpringMVC整合Apache Shiro

    关于什么是Shiro,可以查看这篇文章http://www.cnblogs.com/Laymen/articles/6117751.html 一.添加maven依赖 <dependency> ...

  5. NLP(一)语料库和WordNet

    访问语料库 NLTK数据库的安装:http://www.nltk.org/data.html NLTK语料库列表:http://www.nltk.org/nltk_data/ 内部访问(以Reuter ...

  6. CF #541 E. String Multiplication

    题意: 给定一系列字符串,每次都是后一个字符串和前面的融合,这个融合操作就是原来的串分成独立的,然后把新串插入到这些空格中.问最后,最长的相同连续的长度. 思路: 这道题可以贪心的来,我们压缩状态,记 ...

  7. zoj 5823 Soldier Game 2018 青岛 I

    题目传送门 题意:现在有n个人,现在可以把这n个人分成若干组,只有连续的人才能被分为一组,并且一个组内最多2个人,现在问你 所有组内的最大值-最小值 这个差值最小是多少. 题解: 将每个人的情况3种情 ...

  8. 2017 省赛选拨 火车入站 CSU 1757 模拟

    1757: 火车入站 Submit Page   Summary   Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 512     ...

  9. sed一些常用命令

    [转] http://blog.chinaunix.net/uid-20754793-id-177657.html 下面是我学习sed时参照参考书总结的一些常用sed命令,基本上每条语句都进行了调试1 ...

  10. Elasticsearch(5) --- Query查询和Filter查询

    Elasticsearch(5) --- Query查询和Filter查询 这篇博客主要分为 :Query查询和Filter查询.有关复合查询.聚合查询也会单独写篇博客. 一.概念 1.概念 一个查询 ...