欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-06-06 08:47:49|栏目:JAVA代码|点击:

静态资源路径

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

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/index.html

导入 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({"/", "/index.html"})
  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>

上一篇:JavaWeb项目打开网页出现Session Error的异常解决方案

栏    目:JAVA代码

下一篇:CentOS安装jdk的三种方法

本文标题:SpringBoot静态资源路径配置及主页显示

本文地址:http://www.codeinn.net/misctech/136432.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有