Springboot:thymeleaf使用小记

本文介绍如何在Spring Boot项目中使用Thymeleaf模板引擎,包括添加依赖、控制器数据准备及前端数据渲染,展示文本、样式、列表数据及逻辑判断的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

thymeleaf是新一代的模板引擎,在spring4.0中推荐使用thymeleaf来做前端模版引擎。

之前用过JSP的jstl标签库,现在很少有公司使用jsp了吧,Java代码和前端代码写在一起,看起来很不舒服,维护也很困难,对前端开发人员也不是很友好。thymeleaf使用起来很干净,也不需要过多的配置,加入依赖直接就可以使用。

首先在pom中添加依赖:

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

在controller中准备我们需要在前端渲染的数据。

import com.learning.thymeleaf.model.User;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/lucky")
public class LuckyController {

    @RequestMapping("/show")
    public ModelAndView add(ModelMap map) {
        Map<String, Object> data = new HashMap<>();

        // 文本
        data.put("a", "31岁儿子扮高中生新");
        data.put("b", "苹果声明尊重裁定新");
        data.put("c", "母女奔丧遭杀害");
        //样式
        data.put("d", "margin: 12px 0 0 0;line-height: 150%;color:red");
        data.put("e", "font-family: 微软雅黑;font-size: 18px;color: #3f3f3f;");
        data.put("f", "color: #900b09;border-bottom: 1px solid #900b09");
        
        // 遍历集合
        List<User> userList = new ArrayList<>();
        User user = new User();
        user.setUsername("wuli");
        user.setAge("23");
        user.setSex("男");
        userList.add(user);
        
        User user1 = new User();
        user1.setUsername("linbo");
        user1.setAge("24");
        user1.setSex("男");
        userList.add(user1);

        User user2 = new User();
        user2.setUsername("wangjh");
        user2.setAge("22");
        user2.setSex("男");
        userList.add(user2);

        data.put("user", userList);

        //逻辑运算
        data.put("flag", false);

        map.addAllAttributes(data);
        ModelAndView modelAndView = new ModelAndView("lucky/show");
        modelAndView.addObject("data", data);
        return modelAndView;

    }
}

在这里使用的是ModelAndView发送数据,然后就可以在前端渲染这些数据了。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>luckyshow</title>
</head>
<body>
<!-- 文本和样式渲染-->
<span th:text="${data.a}" th:style="${data.d}"></span>
<span th:text="${data.b}" th:style="${data.e}"></span>
<span th:text="${data.c}" th:style="${data.f}"></span>

<hr>
<!-- 列表数据的遍历-->
<ul th:each="item,index:${data.user}">
    <li>
        姓名: <span class="name" th:text="${item.username}"></span>
        年龄: <span class="age" th:text="${item.age}"></span>
        性别: <span class="sex" th:text="${item.sex}"></span>
    </li>
</ul>

<hr>
<!-- 逻辑渲染-->
<button th:if="${!data.flag}" style="width: 200px;height: 45px;">
    我是按钮点击我
</button>
</body>
</html>

效果如下:

Tips:这里ModelAndView对应的页面是templates下面的lucky/show.html页面。如果没有使用thymeleaf,还使用return跳转页面,那么就是对应static下面的页面。不过要去掉@RestController和@ResponseBody等注解,防止返回的是Json数据。

@Controller
public class HtmlController {
	@GetMapping("/html")
	public String html() {
		return "/index.html";
	}

 

其他常用th标签:

https://blog.youkuaiyun.com/zrk1000/article/details/72667478

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值