SpringBoot_第十一章(Thymeleaf模板引擎)

目录

1:什么是Thymeleaf模板引擎

2:springboot怎使用Thymeleaf

2.1:导入pom文件

2.2:查看ThymeleafAutoConfiguration

 3:Thymeleaf核心语法

4:使用Thymeleaf

5:具体语法练习


1:什么是Thymeleaf模板引擎

在项目中,我们使用前后端分离或者前后端不分离的技术,如果不分离就需要引擎模板

引擎模板跟JSP相似,都是后端的模板解析器,将数据填充到模板页面,返回给前端的技术。

Thymeleaf是一个现代的服务器端Java模板引擎的web和独立的环境,能够处理HTML, XML, JavaScript, CSS,甚至纯文本。

 

2:springboot怎使用Thymeleaf

2.1:导入pom文件

        <!-- 模板引擎 thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

 

2.2:查看ThymeleafAutoConfiguration

约定了配置规范和模板引擎的前缀、后缀

@AutoConfiguration(after = { WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class })
//模板配置 ThymeleafProperties
@EnableConfigurationProperties(ThymeleafProperties.class)
@ConditionalOnClass({ TemplateMode.class, SpringTemplateEngine.class })
@Import({ TemplateEngineConfigurations.ReactiveTemplateEngineConfiguration.class,
		TemplateEngineConfigurations.DefaultTemplateEngineConfiguration.class })
public class ThymeleafAutoConfiguration {



        //Spring资源模板解析器 这里配置了模板引擎
	    @Bean
		SpringResourceTemplateResolver defaultTemplateResolver() {
			SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
			resolver.setApplicationContext(this.applicationContext);
            //前缀
			resolver.setPrefix(this.properties.getPrefix());
            //后缀
			resolver.setSuffix(this.properties.getSuffix());
            //mode 是html
			resolver.setTemplateMode(this.properties.getMode());
			if (this.properties.getEncoding() != null) {
				resolver.setCharacterEncoding(this.properties.getEncoding().name());
			}
			resolver.setCacheable(this.properties.isCache());
			Integer order = this.properties.getTemplateResolverOrder();
			if (order != null) {
				resolver.setOrder(order);
			}
			resolver.setCheckExistence(this.properties.isCheckTemplate());
			return resolver;
		}

}


//查看模板配置文件

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

	private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;

    //前缀 约定了文件位置 在templates目录下
	public static final String DEFAULT_PREFIX = "classpath:/templates/";

    //后缀 约定了文件格式是 .html
	public static final String DEFAULT_SUFFIX = ".html";
      //省略了很多代码
}

 3:Thymeleaf核心语法

 

4:使用Thymeleaf

1:编写controller

@Controller
public class Controller1 {

    @GetMapping(value = "well")
    public String hello(String name, Model model) {
        //前缀: classpath:/templates/
        //后缀: .html
        //真实地址:classpath:/templates/Welcome.html

        //model模型存放参数 model就是Map
        model.addAttribute("msg",name);
        return "Welcome";
    }
}

2:在templates下边编写Welcome.html

<!DOCTYPE html>
<!--引入命名空间 就有了提示-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h3>Welcome页面</h3>
<h5>
  你好呀,  <span th:text="${msg}"></span>
</h5>

</body>
</html>

3:测试访问

http://localhost:8090/well?name=%E5%BC%A0%E4%B8%89

5:具体语法练习

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值