SpringBoot实战(五)之Thymeleaf
Thymeleaf同jsp、volocity、freemarker等共同的职能是MVC模式中的视图展示层,即View。
当然了,SpringBoot中也可以用jsp,不过不推荐这种用法,比较推崇的就是使用Thymeleaf。
关于Thymeleaf学习,建议参考官方文档:https://www.thymeleaf.org/documentation.html
官方文档例子,应有尽有。
之前接触过Thymeleaf是因为公司项目初建期间用过它搭建过测试环境,后来根据领导的指示,需要快速开发,而且当时对于Thymeleaf不是十分了解,当时对于它的了解认识只是展示前端数据的,同jsp职能一样,当然这也是它们的共性。比较详细的深入了解和使用,是帮助一位学妹解决毕业论文的项目问题。那时帮助她写了好十几个类代码和对应的十几个html代码。当时感触比较深的是,Thymeleaf是如此的好用,比jsp还好用,jsp要遍历之类,要么加<%%>,或者引用jstl标签库进行数据遍历等。而Thymeleat就不需要。简洁直观,我比较推崇Thymeleaf,特别是在前后端分离的项目,有的时候不用jsp,光纯html+js的确有点吃力,即便使用了vue.js或者angular.js、react.js等虽然增加了便利性,但是了,如果一个项目前端语言不是十分统一的话,对于将来维护成本方面会大大提高,不易维护。
Thymeleaf对于前后端分离,我认为还是不错的。
下面进入demo例子讲解:
一、导入maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.springframework</groupId>
<artifactId>gs-uploading-files</artifactId>
<version>0.1.0</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
二、编写实体
package hello;
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
三、编写Controller
package hello; import java.util.ArrayList;
import java.util.List; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; @Controller
public class TestController { @GetMapping(value="/test")
public String test(Model model) { List<User> userList = new ArrayList<User>();
for (int i = 0; i <10; i++) {
User user =new User();
user.setId(1);
user.setName("张三");
userList.add(user);
} model.addAttribute("users", userList);
return "test";
}
}
四、编写配置文件,将其放置在/src/main/resources下
application.properties
server.port=8080
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML5
五、在src/main/resources目录下新建templates目录并新建html文件
test.htm
<html xmlns:th="http://www.thymeleaf.org">
<body> <h2>用户列表</h2>
<div>
<ul>
<li th:each="user:${users}">
<span th:text="${user.id}"></span>-
<span th:text="${user.name}"></span>-
</li>
</ul>
</div>
</body>
</html>
六、编写启动类
package hello; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }
最后结果如下图所示:

这里没有加入mysql或者其他数据相关的,如果要引用数据库,引用对应数据库的依赖和配置对应的数据库连接池即可。
这里可以参考我的关于Springboot+MyBatis+Thymeleaf
示例地址为:https://github.com/youcong1996/springboot-mybatis-demo
SpringBoot实战(五)之Thymeleaf的更多相关文章
- 千锋很火的SpringBoot实战开发教程视频
springboot是什么? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员 ...
- [原创].NET 分布式架构开发实战五 Framework改进篇
原文:[原创].NET 分布式架构开发实战五 Framework改进篇 .NET 分布式架构开发实战五 Framework改进篇 前言:本来打算这篇文章来写DAL的重构的,现在计划有点改变.之前的文章 ...
- SpringBoot实战 之 异常处理篇
在互联网时代,我们所开发的应用大多是直面用户的,程序中的任何一点小疏忽都可能导致用户的流失,而程序出现异常往往又是不可避免的,那该如何减少程序异常对用户体验的影响呢?其实方法很简单,对异常进行捕获,然 ...
- SpringBoot整合Jsp和Thymeleaf (附工程)
前言 本篇文章主要讲述SpringBoot整合Jsp以及SpringBoot整合Thymeleaf,实现一个简单的用户增删改查示例工程.事先说明,有三个项目,两个是单独整合的,一个是将它们整合在一起的 ...
- SpringCloud---熔断降级理解、Hystrix实战(五)
SpringCloud---熔断降级理解.Hystrix实战(五) https://www.cnblogs.com/qdhxhz/p/9581440.html https://blog.csdn.ne ...
- apollo客户端springboot实战(四)
1. apollo客户端springboot实战(四) 1.1. 前言 经过前几张入门学习,基本已经完成了apollo环境的搭建和简单客户端例子,但我们现在流行的通常是springboot的客户端 ...
- java框架之SpringBoot(4)-资源映射&thymeleaf
资源映射 静态资源映射 查看 SpringMVC 的自动配置类,里面有一个配置静态资源映射的方法: @Override public void addResourceHandlers(Resource ...
- SpringBoot Web开发(4) Thymeleaf模板与freemaker
SpringBoot Web开发(4) Thymeleaf模板与freemaker 一.模板引擎 常用得模板引擎有JSP.Velocity.Freemarker.Thymeleaf SpringBoo ...
- Python爬虫实战五之模拟登录淘宝并获取所有订单
经过多次尝试,模拟登录淘宝终于成功了,实在是不容易,淘宝的登录加密和验证太复杂了,煞费苦心,在此写出来和大家一起分享,希望大家支持. 温馨提示 更新时间,2016-02-01,现在淘宝换成了滑块验证了 ...
随机推荐
- 撩课-Web大前端每天5道面试题-Day16
1.for循环中的作用域问题? 写出以下代码输出值,尝试用es5和es6的方式进行改进输出循环中的i值. ; i<=; i++) { setTimeout(function timer() { ...
- java.sql.SQLException: Io 异常: Invalid number format for port number
java.sql.SQLException: Io 异常: Invalid number format for port number jdbc数据库链接配置没有写端口号 要检查jdbc的配置 ...
- 系统分析与设计 homework2
1. 简述瀑布模型.增量模型.螺旋模型(含原型方法)的优缺点. 瀑布模型 优点: 降低了软件开发的复杂度,提高软件开发过程中的透明性,提高软件开发的可管理性. 为项目提供了按阶段划分的检查点. 当前一 ...
- spring mybatis 关于 basepackage 和 mapperLocations 的通配符匹配实例
SqlSessionFactoryBean mapperLocations 注意下面几点 classpath* mapperLocation 起始路径不能有 * ,如 dm* 就不行 ** list ...
- log4php使用及配置
log4php使用及配置 1.在项目中加入log4php包 2.log4php配置 在项目配置包中添加logger_config.xml配置文件: logger_config.xml配置文件添加代码如 ...
- JDK的安装与卸载
1.jdk 下载链接:http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html 2.在 ...
- Cookie介绍
1.Http协议与Cookie Cookie(小量信息)是HTTP协议指定的!先由服务器保存Cookie到浏览器,而下次浏览器请求服务器时把上一次请求得到Cookie再归还给服务器 由服务器创建保存到 ...
- BZOJ3193: [JLOI2013]地形生成
传送门 Sol 第一问可以考虑按照山的高度从大到小放 但是这样如果遇到高度相同的就不好考虑,那么同时要求数量限制从小到大 这样每次放的时候后面的一定不会影响前面,并且高度相同的时候前面能放的位置后面的 ...
- 关于构造器和Serlvet的知识点
关于java的构造器方法: 1.java构造方法可以有任何的访问修饰符:public.protected.private或者没有修饰(通常被package或者friendly调用)但是不能有非访问性质 ...
- Angular入门教程二
4 功能介绍 4.1数据绑定 AngularJS的双向数据绑定,意味着你可以在Mode(JS)中改变数据,而这些变动立刻就会自动出现在View上,反之亦然.即:一方面可以做到model变化驱动了DOM ...