zoukankan      html  css  js  c++  java
  • Thymeleaf--起步

      Spring官方支持的服务的渲染模板中,并不包含jsp。而是Thymeleaf和Freemarker等,而Thymeleaf与SpringMVC的视图技术,及SpringBoot的自动化配置集成非常完美,几乎没有任何成本,你只用关注Thymeleaf的语法即可。

    Thymeleaf的特点

    • 动静结合:Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

    • 开箱即用:它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。

    • 多方言支持:Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

    • 与SpringBoot完美整合,SpringBoot提供了Thymeleaf的默认配置,并且为Thymeleaf设置了视图解析器,我们可以像以前操作jsp一样来操作Thymeleaf。代码几乎没有任何区别,就是在模板语法上有区别

         

     

    pom.xml

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

    application.yml   (encoding:UTF-8解决中文乱码)

    spring:
       #开始thymeleaf设置
       thymeleaf:
       #禁用模板缓存
          cache: false
     #设置文字消息
       messages:
          encoding: UTF-8
          basename: message_zh_CN

    controller:

    @Controller
    @RequestMapping("/test")
    public class MyThymeleaf {
    
        @GetMapping("/h")
        public String t(Model model){
            String title="标题";
            String message="first thymeleaf !!";
            model.addAttribute("message",message);
            model.addAttribute("title",title);
            return "index";
        }
    }

    index.html

    <!doctype html>
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:th="http://www.thymeleaf.org">       ------ 声明当前文件是 thymeleaf, 里面可以用th开头的属性
        <head>
            <meta charset="UTF-8">
            <title>首页</title>
        </head>
        <body>
            <h1 th:text="#{title}"></h1>
    
            <h1 th:text="${message}"></h1>
        </body>
    </html>

    启动后访问:http://localhost:8080/test/h

  • 相关阅读:
    Docker中查看Mysql数据库中的各环境参数
    Hbase shell 输入无法使用退格键删除解决办法
    HBase启动时报错:/bin/java: No such file or directory6/bin/../bin/hbase: line 412: /usr/local/jdk1.8.0_152/bin/java
    SSH无密码验证
    详解分布式应用程序协调服务Zookeeper
    zookeeper的原理及使用
    Hadoop、Yarn和vcpu资源的配置
    一文让您全面了解清楚HBase数据库的所有知识点,值得收藏!
    基于Docker一键部署大规模Hadoop集群及设计思路
    PHP ServerPush (推送) 技术的探讨【转】
  • 原文地址:https://www.cnblogs.com/crazy-lc/p/12249162.html
Copyright © 2011-2022 走看看