SpringBoot+Thymeleaf实现数据渲染

Thymeleaf简介

1.是什么

Thymeleaf是spring boot推荐使用的模板语法,它可以完全替代 JSP 。
从代码层次上讲:Thymeleaf是一个java类库,它是一个xml/xhtml/html5的模板引擎,可以作为mvc的web应用的view层。

2.优点

开箱即用,它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、改jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言;
Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
有网无网的情况下模版页面都可以执行,美工的页面拿来就可以用,相对jsp减少了额外的标签,页面也更加简洁。

3.常用标签

属性作用优先级(数字越小,优先级越高)
th:text设置当前元素的文本内容7
th:value设置当前元素的value值,类似修改指定html标签属性的还有th:src,th:href6
th:each遍历循环元素,和th:text或th:value一起使用2
th:if条件判断3

4.标准表达式语法

语法含义
${…}变量表达式(最常用)
@{…}链接表达式
#{…}消息表达式
~{…}代码块表达式
*{…}选择变量表达式

SpringBoot+Thymeleaf交互

1.交互代码

在controller中绑定数据,并将数据返回到指定页面


@Controller
@RequestMapping("/mapImage/mapImage")
public class MapImageController extends BaseController
{
    private String prefix = "mapImage/mapImage";

    @Autowired
    private MapImageController Service MzMapImageService;

    @Autowired
    private CommunitymanageService CommunitymanageService;
    
    @GetMapping()
    @ApiOperation("查询社区")
    public String mapImage(ModelMap model)
    {
        List<Communitymanage> list = CommunitymanageService.queryCommunityInfo();
        model.put("list", list);
        return prefix + "/mapImage";
    }
}

在mapImage.html页面中,通过thymeleaf的语法将数据渲染

<html lang="zh" xmlns:th="http://www.thymeleaf.org">

<div>
	<p class="contentTitle">社区信息</p>
	<p th:each="list:${list}" th:text="${list.communityname}")></p>
</div>

2.关键代码解析

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值