springboot集成Thymeleaf(一)

springboot集成Thymeleaf(一)传统Java WEB工程时,我们可以使用JSP页面模板语言,但是在SpringBoot中已经不推荐使用了。SpringBoot支持如下页面模板语言Thymeleaf FreeMarker Velocity Groovy JSP …………其中Thymeleaf是SpringBoot官方所推荐使用的,接下来说说Thymeleaf使用。一、特点动静结合: 1、Thym…

大家好,又见面了,我是你们的朋友全栈君。

传统Java WEB工程时,我们可以使用JSP页面模板语言,但是在SpringBoot中已经不推荐使用了。SpringBoot支持如下页面模板语言

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • JSP
  • …………

其中Thymeleaf是SpringBoot官方所推荐使用的,接下来说说Thymeleaf使用。

一、特点

动静结合:

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

多方言支持:

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

与SpringBoot完美整合:

    1、与SpringBoot完美整合,SpringBoot提供了Thymeleaf的默认配置

    2、并且为Thymeleaf设置了视图解析器,我们可以像以前操作jsp一样来操作Thymeleaf

二、使用

添加依赖:

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

查看thymeleaf源码:idea版本不同查看源码快捷键可能不一样,

查看源码:https://blog.csdn.net/BlackPlus28/article/details/101014267

与解析JSP的InternalViewResolver类似,Thymeleaf也会根据前缀和后缀来确定模板文件的位置:

通过查看源码得知:

springboot集成Thymeleaf(一)

会在templates文件夹下找出.html文件。

数据显示

在resources文件中创建templates文件夹,并创建html文件。如图:

springboot集成Thymeleaf(一)

 

创建一个Controller,并进行返回,名称与html名称保持一致。

在类上加 @Controller注解
@Controller
public class MyController {

    @RequestMapping("/hello")
    public String hello(Model model) {
        model.addAttribute("name","ssss");
        return "Hello";
    }

编写html文件:h1标签,仅仅演示效果

效果图:

springboot集成Thymeleaf(一)

 

使用th之前需要引入命名空间:通过${}这种形式进行取值

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1 th:text="${name}">Hello</h1>
</body>
</html>

这里就集成好themeleaf。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/147165.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月13日 下午9:46
下一篇 2022年6月13日 下午9:46


相关推荐

  • 当正经的雪佬,开始邪修Nano-Banana,你确定不看下?

    当正经的雪佬,开始邪修Nano-Banana,你确定不看下?

    2026年3月13日
    2
  • 开源 微商分销系统 php,[PHP程序] 微商新零售分销平台源码Thinkphp内核 产品营销推广神器…

    开源 微商分销系统 php,[PHP程序] 微商新零售分销平台源码Thinkphp内核 产品营销推广神器…源码介绍一个新零售的派单工具,有了平台,会让客户主动加你买货,100%成交,还会积极帮你转介绍,让你不仅仅获得派单,还能建立自己的商友圈,积累强大的人脉。平台的机制中一共分为13个商友星级,从一星到十三星,每升一星就会获得大量的平台派单,升到13星一共可获得百万笔零售订单。平台没有行业限制,无论你是微商,电商还是实体,无论你是卖衣服、化妆品还是食品,都可以使用平台。同行可以合作,异业可以联盟,不用…

    2022年5月17日
    40
  • resultMap和resultType 的区别?

    resultMap和resultType 的区别?resultMap 和 resultType

    2026年3月16日
    2
  • scanf的返回值

    scanf的返回值scanf 的返回值函数 scanf 的返回值就是 所输入的数据与格式字符串中匹配次数 简单来说就是 它返回已成功赋值的数据项数 出错时则返回 EOF 注 EOF EndOfFile 是一个预定义的常量 等于 1 例如 可以通过以下程序证实 include amp amp lt stdio h amp amp gt 证实函数 scanf 的返

    2026年3月18日
    4
  • calendar类的日期加减

    calendar类的日期加减calendar 类 日期加减 Java 代码 public nbsp class nbsp test1 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp public nbsp static nbsp void nbsp main String nbsp args nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp Calendar nbsp c nbsp nbsp Calendar getInstance nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp int nbsp year c get Calendar YEAR nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp

    2026年1月21日
    2
  • 【工作技巧】WinRAR去除广告

    【工作技巧】WinRAR去除广告一、下载软件1.1、进入WinRAR国内官网,根据个人电脑系统位数进行下载对应的安装包。傻瓜式安装WinRAR,全部点击下一步。1.3、进入ResourceHacker官网,点击Download,然后点击EXEinstall,下载安装包。傻瓜式安装ResourceHacker,全部点击下一步。二、修改WinRAR.exe2.1、打开ResourceHacker程序,使用快捷键【Ctrl+O】选择WinRAR.exe文件。一般路径为【D:\ProgramFiles\WinRAR\Wi

    2022年5月9日
    48

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号