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">-->

加上“/”表示绝对路径

原文地址:https://www.cnblogs.com/kingsonfu/p/11516967.html

时间: 2024-08-27 08:43:55

springboot + thymeleaf静态资源访问404的相关文章

Tomcat静态资源访问404问题

Tomcat静态资源访问404问题     前言 该问题并不是eclipse或者是tomcat的问题,究其原因是自己资源路径写的有问题   分析 我的开发工具是eclipse,而不管是什么工具,项目都会有一个根路径 如图(右击项目->Properties->Deployment assembly) 注意source一栏就是根路径 如下 /src /target/m2e-wtp/web-resources /WebContent 那么根路径有什么用呢 例如,一般会把前端的资源放在项目中的WebC

SpringBoot配置静态资源访问与本地路径的映射

1.配置工程访问路径映射本地路径: package com.liuyanzhao.chuyun.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annot

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

1.项目目录结构 2.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apach

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

在此记录一下这十几天的学习情况,卡在模板引擎这里已经是四天了. 对Springboot的配置有一个比较深刻的认识,在此和大家分享一下初学者入门Spring Boot的注意事项,如果是初学SpringBoot,或者有意向学习Springboot的朋友,这篇文章可以简单的来帮助你,处理一些不必要的麻烦. 开发环境: IDea JDK1.8 SpringBoot2+ Maven3.5 1.配置Maven 首先我们打开IdeA我们从新建一个项目开始.SpringBoot是基于Maven来管理Jar包的.

《SpringBoot静态资源访问》

SpringBoot 1.X 版本和 SpringBoot 2.X 版本在静态资源访问上有一些区别,如果直接从 1.X 升级到 2.X 肯定是有问题的.这篇文章就来讲讲这方面问题,也是项目中的坑. 时间关系,待续 原文地址:https://www.cnblogs.com/xums/p/9285969.html

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

2.4.4 SpringBoot静态资源访问(9) Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则 /static  /public  /resources  /META-INF/resources 可以在src/main/resources目录下创建static,在该位置放置一个图片文件. 启动程序后,尝试访问http://localhost:8080/D.JPG,如能显示图片,配置成功. 2.5 SpringBoot整合freemarker视图

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

Springboot 再模板引擎中引入Js等文件,出现服务器拒绝访问的错误,需要配置过滤器 静态资源访问配置 @Configuration @EnableWebMvc public class StaticResourceConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHand

SpringMVC REST 风格静态资源访问配置

1 在web.xml中使用默认servlet处理静态资源,缺点是如果静态资源过多,则配置量会比较大,一旦有遗漏,则会造成资源无法正常显示或404错误. <!-- 静态资源访问控制 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-

Spring MVC 读取静态资源时404错误

背景:web.xml配置时拦截策略是拦截所有请求: <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-