视图解析-Thymeleaf初体验

一、Thymeleaf介绍

Thymeleaf是一种服务器端Java模板引擎,适用于构建基于MVC框架的Web应用程序。它的设计目标是在服务器端生成HTML页面,将数据与HTML页面进行动态绑定,以产生最终的可视化内容。

Thymeleaf的主要特点包括:

  1. 自然模板:Thymeleaf模板与常规HTML是兼容的,非Thymeleaf标签可以在Thymeleaf模板中正常工作,而没有额外的模板解析步骤。

  2. 更好的可读性:Thymeleaf模板具有良好的可读性,可以读作普通的HTML文件,即使没有模板引擎的上下文。

  3. 内置表达式:Thymeleaf提供了强大的表达式语言(Thymeleaf Expression Language,简称Thymeleaf EL),可以在模板中访问和操作数据。

  4. 语法短小精悍:Thymeleaf标签的语法简单明了,易于学习和使用。

  5. 扩展性:Thymeleaf支持自定义标签、表达式和工具,可以根据项目需求进行功能扩展。

 二、Thymeleaf使用

2.1 引入Thymeleaf相关依赖(引入Starter)

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

2.2  默认放置处为:classpath:/templates,模板的默认后缀名为:.html

SpringBoot在ThymeleafProperties.class 里面进行了默认配置:

@ConfigurationProperties(prefix = "spring.thymeleaf")
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";//文件的后缀名

如果想要修改默认的模板位置和后缀名,可以在Spring Boot的配置文件中进行设置。以下是一个示例的application.properties配置文件内容:

spring.thymeleaf.prefix=classpath:/mytemplates/
spring.thymeleaf.suffix=.tpl

2.3  创建html页面,配置Thymeleaf的命名空间(xmlns:th="http://www.thymeleaf.org") 

xmlns:th="http://www.thymeleaf.org"这个声明使你能够在HTML文档中使用Thymeleaf的标签和表达式。通过使用Thymeleaf的特定命名空间,你可以在模板中引用Thymeleaf的功能和属性。

/templates/success.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1 th:text="${msg}">nice</h1>
<h2>
    <a href="www.baidu.com" th:href="${link}">去百度</a>  <br/>
    <a href="www.google.com" th:href="@{/link}">去百度</a>
</h2>
</body>
</html>

2.4  编写相应的controller

@Controller
public class ViewTestController {
    @GetMapping("/hello")
    public String hello(Model model){
        //model中的数据会被放在请求域中 request.setAttribute("a",aa)
        model.addAttribute("msg","你好~");
        model.addAttribute("link","https://blog.csdn.net/");
        return "success";
    }
}

这样通过访问localhost:8080/hello,即可跳转到success.html页面

三、基本语法

3.1 表达式

表达式名字语法用途
变量取值${…}获取请求域、session域、对象等值
选择变量*{…}获取上下文对象值
消息#{…}获取国际化等值
链接@{…}生成链接
片段表达式~{…}jsp:include 作用,引入公共页面片段

3.2 属性优先级 

OrderFeatureAttributes
1Fragment inclusionth:insert th:replace
2Fragment iterationth:each
3Conditional evaluationth:if th:unless th:switch th:case
4Local variable definitionth:object th:with
5General attribute modificationth:attr th:attrprepend th:attrappend
6Specific attribute modificationth:value th:href th:src ...
7Text (tag body modification)th:text th:utext
8Fragment specificationth:fragment
9Fragment removalth:remove

四、Thymeleaf内联写法 

<p>Hello, [[${session.user.name}]]</p>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值