当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot静态资源路径配置及主页显示

SpringBoot静态资源路径配置及主页显示

2020年05月12日  | 移动技术网IT编程  | 我要评论
静态资源路径静态资源支持放在以下路径中,访问优先级从上到下:classpath:/meta-inf/resources/classpath:/resources/classpath:/static/

静态资源路径

静态资源支持放在以下路径中,访问优先级从上到下:

classpath:/meta-inf/resources/
classpath:/resources/
classpath:/static/ # 默认路径
classpath:/public/

其中 classpath 为 src/main/resources 目录。

请求地址为:http://localhost:8080/xx.js

首页

文件位置:

classpath:/static/favicon.ico
classpath:/templates/

导入 thymeleaf 模板引擎依赖:

<dependencies>
  <dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter</artifactid>
  </dependency>
  <dependency>
    <groupid>org.thymeleaf</groupid>
    <artifactid>thymeleaf-spring5</artifactid>
  </dependency>
  <dependency>
    <groupid>org.thymeleaf.extras</groupid>
    <artifactid>thymeleaf-extras-java8time</artifactid>
  </dependency>
</dependencies>

定义请求控制器:

@controller
public class indexcontroller {
  @requestmapping({"/", "/"})
  public string index(model model){
    model.addattribute("msg", "hello, thymeleaf!");
    return "index";
  }
}

加入模板内容显示首页:

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8">
  <title>index page</title>
</head>
<body>
	<h1>首页</h1>
	<div th:text="${msg}"></div>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网