SpringBoot—整合thymeleaf

8 篇文章 0 订阅
2 篇文章 0 订阅

引言:

好几个项目用到这个thymeleaf,在这里记录下心得。

Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎。类似JSP,FreeMaker等。与其它模板引擎相比,Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用,源码的可阅读性高。

一、maven依赖

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

二、contorller层简单写了一下

@GetMapping("/")
    public String index(ModelMap modelMap) {
        modelMap.addAttribute("tableName", "用户信息");
        List<User> userList = new ArrayList<>();
        User u1 = new User();
        u1.setName("it疯子也");
        u1.setAge(22);
        User u2 = new User();
        u2.setName("张三");
        u2.setAge(25);
        userList.add(u1);
        userList.add(u2);
        modelMap.addAttribute("userList", userList);
        return "index";
    }

 

三、新建html页面

新建index.html页面,放在templates文件下,springboot自动配置的视图解析器能找到。

 注:

html标签加上  xmlns:th="http://www.thymeleaf.org",这样才能解析thymeleaf的标签语法。

标签上使用th:text="${xxx}",能从modelMap中取出对应的值,并替换标签html内容。

th:each="xxx: ${xxx}",这是thymeleaf遍历数组的语法,会其他模板引擎或者vue的一眼能会。

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<meta charset="UTF-8">
	<title></title>
</head>
<body>
<h3 th:text="${tableName}"></h3>
<table>
	<thead>
		<tr>
		  <th>姓名</th>
		  <th>年龄</th>
		</tr>
	</thead>
	<tr th:each="user : ${userList}">
	   <td th:text="${user.name}"></td>
	   <td th:text="${user.age}"></td>
	</tr>
</table>
</body>
</html>

四、运行效果

springboot整合thymeleaf很简单就完成了,当然要熟练运用就要掌握很多thymeleaf语法,俗话说得好熟能生巧还是要多用。学习任何新技术的时候都是踩过很多坑趟过来的。

整合这块就这么完了,后续会跟新常用的thymeleaf语法。并把链接贴在下面

五、常用的thymeleaf语法

(1)thymeleaf常用工具对象

待更。。。。

 

                                                                                                           向上的路并不拥挤,而大多数人选择了安逸。--it疯子也

Spring Boot整合Thymeleaf是一种常见的做法,用于在Spring Boot应用中利用Thymeleaf作为模板引擎,提供动态网页功能。Thymeleaf是一个强大的、现代的Web模板引擎,支持HTML5XML。 以下是整合步骤: 1. 添加依赖:在你的`pom.xml`文件中添加Thymeleaf及其Spring Boot支持的依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> ``` 2. 配置视图解析器:在`application.properties`或`application.yml`中设置Thymeleaf的视图位置: ``` spring.thymeleaf.views.location=classpath:/templates/ ``` 3. 创建模板目录:在项目的`src/main/resources/templates`目录下创建HTML模板文件。 4. 使用Thymeleaf标签:在模板文件中,你可以使用Thymeleaf的表达式语言(EL)和特殊语法,如条件语句、迭代等。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>My Spring Boot App</title> </head> <body> <h1 th:text="${message}">Hello, World!</h1> </body> </html> ``` 5. 在Controller中返回模型数据并指定视图:例如: ```java import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String home(Model model) { model.addAttribute("message", "Welcome to Spring Boot with Thymeleaf!"); return "home"; // 指定模板名称 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值