程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(4)

SpringBoot_thymeleaf模板引擎

发布于2021-05-29 20:55     阅读(679)     评论(0)     点赞(15)     收藏(3)


1.模板引擎

比如javaweb中的jsp就是一个模板引擎。
为的是可以在html上展示一些特殊的数据的一些特殊的语法
现在比较流行的模板引擎:Thymeleaf、FreeMaker、Velocity还有jsp。

2.Thymeleaf介绍

特点:
1.动静分离: Thymeleaf选用html作为模板页,这是任何一款其他模板引擎做不到的!Thymeleaf使用html通过一些特定标签语法代表其含义,但并未破坏html结构,即使无网络、不通过后端渲染也能在浏览器成功打开,大大方便界面的测试和修改。

2.开箱即用: Thymeleaf提供标准和Spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、改JSTL、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
在这里插入图片描述
在这里插入图片描述

3.Thymeleaf

1.导入jar包

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

2.编写controller

@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
    @RequestMapping("/success")
    public String test1(Map<String, Object> map){
        map.put("hello", "<h3>你好</h3>");
        map.put("users", Arrays.asList("Zzz", "ZZy"));

        return "success";
    }
}

3.编写页面

在这里插入图片描述
必须在templates下面编写.html页面

<body>
<h3>success</h3>
<div th:text="${hello}"></div>
<!--标签一起解析-->
<div th:utext="${hello}"></div>

<hr>

<h4 th:text="${user}" th:each="user:${users}"></h4>
<hr>

<h4>
    <span th:each="user:${users}">[[${user}]]</span>
</h4>
</body>

对应的是javaweb的jsp和springmvc的视图解析器
根据视图层的返回success,默认返回的是templates下面的success.html页面

在这里插入图片描述

4.thymeleaf语法

在这里插入图片描述
表达式语法

在这里插入图片描述

原文链接:https://blog.csdn.net/qq_43141726/article/details/117325700



所属网站分类: 技术文章 > 博客

作者:怎么没有鱼儿上钩呢

链接:http://www.javaheidong.com/blog/article/207301/c2f5cc714644d53fbb75/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

15 0
收藏该文
已收藏

评论内容:(最多支持255个字符)