springboot、thymeleaf简单整合

  • tmplates下放html文件
  • 一个html文件如果要使用thymeleaf语法,需要引入thymeleaf名称空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
  • static下放静态资源

一、引入依赖

org.springframework.boot » spring-boot-starter-thymeleaf » 2.1.8.RELEASE

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

热启动
页面经常要修改,添加了热启动后不用每次都能重启项目来使修改生效,只需要重新编译html文件 ctrl+shift+F9 或者重新编译项目 ctrl+F9,修改即可生效

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

二、配置

开发时关闭缓存,上线后打开缓存
application.properties

spring.thymeleaf.cache=false

三、测试

tmplates下新建index.html,启动项目,访问项目,访问到index.html

因为不带任何访问路径时,springboot默认访问到index.html

若想访问http://127.0.0.1:8080/index.html也访问到index.html,需要在controller里配置,访问其他html文件时,也是按同样方法配置

@Controller
public class IndexController {

    @RequestMapping({"/", "/index", "/index.html"})
    public String index() {

        return "index";
    }
}

如果访问不到static下的静态资源,可以加这段代码

@Configuration
public class SpringMvcSupport implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值