03-SpringBoot_thymeleaf简单用法

一、导入静态资源

1.1 静态资源优先级

可以直接访问:localhost:8080/1.js

在这里插入图片描述

二、首页

方式一:

直接在static目录中添加index.html即可

方式二:需要先学下一个模板引擎

templates中的html页面,但是只能通过Controller访问

图标管理:

1、springboot:2.1.7

2、application.properties

# 关闭默认图标
spring.mvc.favtion.enabled=false

3、将自己的图标导入项目,放在resources/public下面即可,命名为:favtion.ico

4、然后清除浏览器缓存

三、模板引擎Thymeleaf

1、导入依赖
<dependency>
	<groupId>org.thymeleaf</groupId>
	<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
	<groupId>org.thymeleaf.extras</groupId>
	<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
2、编写html

​ 在templates目录中写html。然后再Controller中写跳转链接即可

源码: 前缀-后缀

public class ThymeleafProperties {

	private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
	// 前缀
	public static final String DEFAULT_PREFIX = "classpath:/templates/";
	// 后缀
	public static final String DEFAULT_SUFFIX = ".html";
3、thymeleaf的语法
3.1、添加 命名空间
<html lang="en" xmlns:th="http://www/thymeleaf.org">

官方文档

thymeleaf官方文档

3.2、取消息,类比Vue
<h1 th:text="${msg}"></h1>
3.3、常用语法

text 、utext:转义

// 传参数:<h1>hello,springboot</h1>

<h1 th:text="${msg}"></h1> <!--<h1>hello SpringBoot~xd</h1>-->
<h1 th:utext="${msg}"></h1>	<!--hello SpringBoot~xd-->

each:遍历

<!--model.addAttribute("users", Arrays.asList("xd","xd1","xd2"));-->
<h3 th:each="user:${users}" th:text="${user}"></h3>
<h3 th:each="user:${users}" >[[${user}]]</h3>

其余的看源码或者官方文档

四、装配扩展SpringMVC

当我们需要扩展SpringMvc的时候,只用实现就完事了!

1、自定义视图跳转

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    //自定义视图跳转
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/xd").setViewName("test");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值