Spring Boot中Thymeleaf的初步使用
目录
理论
演示
理论
使用TemplateEngine是Spring Boot中推荐的,他的作用是:
把模板(如html界面)和数据匹配好,然后输出,发给用户。
而不是传统的使用jsp进行操作
模版和数据交给引擎,最后给出一个界面给用户。
Thymeleaf语法简单,功能强大;
xmlns:th="http://www.thymeleaf.org"
通过这个能在写HTML中获取Thymeleaf的提示
演示
运行截图如下:
程序结构如下:
源码如下:
HelloControler.java
package com.jar.demo.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.Arrays;
import java.util.Map;@Controller
public class HelloController {@ResponseBody@RequestMapping("/hello")public String hello(){return "Hello World";}//查出一些数据,显示到页面上@RequestMapping("/success")public String success(Map<String, Object> map){map.put("hello", "你好");map.put("HowAreYou", "<h1>咋了</h1>");map.put("users", Arrays.asList("zhangsan", "lisi", "wangwu"));return "success";}
}
JarApplication.java
package com.jar.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class JarApplication {public static void main(String[] args) {SpringApplication.run(JarApplication.class, args);}}
success.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><h1>成功</h1><div th:text="${hello}"></div><!-- 使用th任意属性,替换原有属性 --><div id="div01" class="myDiv" th:id="${hello}" th:class="${hello}" th:text="${hello}">欢迎</div><!-- th:text 和 th:utext的区别 --><div th:text="${HowAreYou}"></div><div th:utext="${HowAreYou}"></div><br><br><!-- 遍历标签 --><h4 th:text="${user}" th:each="user:${users}"></h4><br><h4><span th:each="user:${users}">[[${user}]]</span></h4><!-- 结束 --><h1>Hello</h1></body>
</html>
Spring Boot中Thymeleaf的初步使用相关推荐
- js文件中怎么使用thymeleaf标签_007、Spring Boot集成Thymeleaf模板引擎
1. Thymeleaf 介绍 Thymeleaf 是适用于 Web 和独立环境的现代服务器端 Java 模板引擎. Thymeleaf 的主要目标是为您的开发工作流程带来优雅的自然模板 - 可以在浏 ...
- 8.Spring Boot中使用thymeleaf
Spring Boot中使用thymeleaf Spring Boot支持FreeMarker.Groovy.Thymeleaf和Mustache四种模板解析引擎,官方推荐使用Thymeleaf. s ...
- 再谈Spring Boot中的乱码和编码问题
编码算不上一个大问题,即使你什么都不管,也有很大的可能你不会遇到任何问题,因为大部分框架都有默认的编码配置,有很多是UTF-8,那么遇到中文乱码的机会很低,所以很多人也忽视了. Spring系列产品大 ...
- Spring Boot中Starter是什么
比如我们要在Spring Boot中引入Web MVC的支持时,我们通常会引入这个模块spring-boot-starter-web,而这个模块如果解压包出来会发现里面什么都没有,只定义了一些POM依 ...
- java中的controller_详解Spring Boot中Controller用法
Controller Controller是SpringBoot里最基本的组件,他的作用是把用户提交来的请求通过对URL的匹配,分配个不同的接收器,再进行处理,然后向用户返回结果.他的重点就在于如何从 ...
- java 模板引擎_极简 Spring Boot 整合 Thymeleaf 页面模板
点击"牧码小子"关注,和众多大牛一起成长! 关注后,后台回复 java ,领取松哥为你精心准备的技术干货! 虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在 ...
- Spring Boot集成Thymeleaf模板引擎
一.Thymeleaf 模板介绍 Spring Boot 推荐使用Thymeleaf 来代替传统开发中的JSP,那么什么是Thymeleaf 模板引擎呢?下面就来简单的介绍一下. Thymeleaf ...
- Spring Boot整合Thymeleaf模板引擎
转载自 Spring Boot整合Thymeleaf模板引擎 什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMa ...
- Spring Boot中带有CKEditor的AJAX
1.概述 在本文中,我们将介绍如何在Spring Boot中使用CKEditor . 在本教程中,我们将导入一个包含大量数据的XML文档,对使用GET请求将一组数据加载到CKEditor实例的能力进行 ...
最新文章
- h5 和native 交互那些事儿
- 国内知名 IT 公司前端团队
- while和for循环
- Windows 下修改 MySQL 编码为 utf8
- Kerio Network Monitor
- IDEA如何导入项目
- 网络七层协议,五层协议概述
- 山大软件项目管理复习整理
- 央视《家有妙招》整理版,共250招,值得收藏
- Nginx 重定向 80 到443
- 解决Minimum supported Gradle version is 7.0.2. Current version is 6.8.
- 浅谈fiddler抓包工具中自动断点——Before Requests与After Responses断点功能
- 如何用python进行相关性分析_Python 数据相关性分析
- 代码故障检测列表(持续更新)
- 【mysql】查询中英文名称拼接处理
- 微擎系统跟换服务器和域名,微擎修改服务器域名
- 【C++】学籍信息管理
- 智能优化算法之 差分进化算法
- 中国纯电动车新车质量蔚来、特斯拉、欧拉列前三;长城汽车计划将P系列皮卡推向全球市场 | 美通企业日报...
- 欧盟RAPEX电子烟类通报情况