Springboot的中static和templates的区别

在src/main/resources下面有两个文件夹,static和templates。springboot默认  static中放静态页面,而templates中放动态页面 

1.依赖设置(选中web)

在static和templates下分别建两个htm文件

首先我们通过localhost:8080/demo1.html访问成功。访问demo2.html不成功 

通过编写下面代码,localhost:8080/index访问demo1.html成功

@Controller
@RequestMapping()
public class IndexController {
    @RequestMapping("/index")
    public String indexDemo(){
        return "demo1.html";
    }
}

2.增加Thymeleaf模板引擎依赖后。

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

编写下面代码,localhost:8080/index访问demo2.html成功

@Controller
@RequestMapping()
public class IndexController {
    @RequestMapping("/index")
    public String indexDemo(){
        return "demo2";
    }
}

静态页面的return默认是跳转到/static/目录下,当在pom.xml中引入了thymeleaf组件,动态跳转会覆盖默认的静态跳转,默认就会跳转到/templates/下,注意看两者return代码也有区别,动态没有html后缀

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值