SpringBoot系列七、静态资源文件配置

一、目录介绍:
src/main/java:存放代码
src/main/resources
static:存放静态文件,比如 css、js、image
templates:存放静态页面,比如jsp、html、tpl
config:存放配置文件,application.properties
二、同个文件的加载顺序(静态资源文件)
SpringBoot默认会挨个从META/resource>>resources>>static>>public里面查找是否存在相应的资源,如果有则直接返回。
三、访问不是默认文件下的静态资源文件
引入模版引擎(在pom中添加如下依赖)

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

在src/main/resources文件下创建html文件夹,在html文件夹下创建index.html文件,要访问index.html文件,需要在application.properties中添加下列配置:

spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/html/

访问http://localhost:8080/index.html即可。
四、通过controller跳转页面
在templates文件夹下创建测试页面Hello.html,创建controller
HelloController如下:

@Controller
public class HelloController {

    @RequestMapping(value = "/hello")
    public Object hello(){
        return "hello";
    }
}

页面访问http://localhost:8080/hello即可进入hello.html页面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值