SpringMVC整合Thymeleaf
Thymeleaf的介绍
进行JavaWeb开发时主要用到的是JSP,传统的JSP需要在页面中加入大量的JSTL标签,这些标签只能运行在服务器中,前端开发人员维护这些页面比较困难,页面加载速度也比较慢。 Thymeleaf是一种全新的页面模板引擎,在Thymeleaf中使用的标签都是基本的HTML标签,可以脱离服务器独立运行,这样前端开发人员可以维护静态页面,后台开发人员将数据绑定上去,利于分工合作,Thymeleaf的语法也比较简洁优雅,比较容易使用。
SpringMVC整合Thymeleaf
下面介绍下在SpringMVC框架中如何使用Thymeleaf:<br>
1. 首先使用Maven导入需要的库:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
说明:spring-context、spring-web、spring-webmvc是Spring和SpringMVC的相关库,thymeleaf是Thymeleaf模板库thymeleaf-spring4是Spring整合Thymeleaf所需的库。
1. 在SpringMVC的配置文件中添加:
<!--thymeleaf模板解析器-->
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<!--前缀配置-->
<property name="prefix" value="/WEB-INF/"></property>
<!--后缀配置-->
<property name="suffix" value=".html"></property>
<!--模板类型-->
<property name="templateMode" value="HTML"></property>
<!--不使用缓存-->
<property name="cacheable" value="false"></property>
<!--编码类型-->
<property name="characterEncoding" value="UTF-8"></property>
</bean>
<!--模板引擎配置-->
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"></property>
</bean>
<!--视图处理器-->
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"></property>
<property name="characterEncoding" value="UTF-8"></property>
</bean>
1. 添加实体类和Controller类
//商品类
public class Goods{
private int id;
private String name;
private double price;
private String type;
//set\get...
}
//商品控制器
@Controller
public class GoodsController {
@RequestMapping("/goods")
public String test1(Model model){
//添加
List<Goods> goods = new ArrayList<>();
goods.add(new Goods(1,"小米9手机",3999,"数码");
goods.add(new Goods(2,"小米8手机",2999,"数码");
goods.add(new Goods(3,"小米7手机",1999,"数码");
model.addAttribute("goods",goods);
return "goods";
}
}
1. 编写Thymeleaf页面
在html标签中加入命名空间:
<html xmlns:th="http://www.thymeleaf.org">
使用表格显示后台数据:
<table>
<tr th:each="g:${goods}">
<td th:text="${g.id}">1</td>
<td th:text="${g.name}">牙膏</td>
<td th:text="${g.price}">200</td>
<td th:text="${g.type}">100</td>
</tr>
</table>
1. 启动服务器,输入URL后可以看到Thymeleaf将后台数据展示到页面中
Thymeleaf的属性用法
前面案例中th:each是将标签进行循环迭代,绑定集合中的数据;th:text是用来绑定每个对象的文本数据。 我们在看看还有哪些常用的属性:
1、变量,这种写法类似EL表达式和OGNL:
<h1 th:text="${msg}">Test</h1>
如果变量是user对象,可以写成:
<h1 th:text="${user.name}">Test</h1>
注意:h1标签之间的内容是静态页面显示的,当连接上服务器后会被替换为th:text中的内容。
2、超链接
使用th:href标签,内容格式是@{...}
如:<a href=”index.html” th:href=”@{http://localhost:8080/list(uid=${user.id})}”>测试</a>
注意:这里list(uid=${user.id})会被替换为list?uid=8类似的格式
3、文本替换
<p th:text="'Welcome , ' + ${user.name} + '!'">xxx</p>
或:
<p th:text="|Welcome , ${user.name}!|">xxx</p>
4、运算符
Java中的算术运算符、关系运算符、逻辑运算符等都可以使用,
不过在使用<和>时由于是特殊符号,需要进行转义:
th:if="${count} > 1"
th:text="'Execution mode is ' + ( (${execMode} == 'dev')? 'Development' : 'Production')"
5、循环
th:each=”变量:${集合对象}”
<table>
<tr th:each="emp : ${empList}">
<td th:text="${emp.id}">1</td>
<td th:text="${emp.name}">张三</td>
<td th:text="${emp.age}">18</td>
</tr>
</table>
6、条件
th:if和th:unless,th:if是条件成立是显示标签,th:unless则相反
下面标签的效果相同,都是当user为空时显示Login超链接
<a th:href=”@{/login}” th:if=”${user==null}”>Login</a>
<a th:href=”@{/login}” th:unless=”${user!=null}”>Login</a>
7、switch
<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="'manager'">User is a manager</p>
<p th:case="*">User is some other thing</p>
</div>
以上就是Thymeleaf的基本用法,可以看到Thymeleaf的用法比较简单,可以很好的代替JSP完成页面开发。
SpringMVC整合Thymeleaf的更多相关文章
- 【springmvc thymeleaf】springmvc整合thymeleaf
概述 Thymeleaf提供了一组Spring集成,使您可以将其用作Spring MVC应用程序中JSP的全功能替代品. 这些集成将使您能够: @Controller像使用JSP一样,将Spring ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- Springboot整合thymeleaf模板
Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
- Spring Boot2 系列教程 (十二) | 整合 thymeleaf
前言 如题,今天介绍 Thymeleaf ,并整合 Thymeleaf 开发一个简陋版的学生信息管理系统. SpringBoot 提供了大量模板引擎,包含 Freemarker.Groovy.Thym ...
- SpringBoot 整合thymeleaf
1.Thymeleaf介绍(官网推荐:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html) Thymeleaf是跟Veloc ...
- (转)Dubbo与Zookeeper、SpringMVC整合和使用
原文地址: https://my.oschina.net/zhengweishan/blog/693163 Dubbo与Zookeeper.SpringMVC整合和使用 osc码云托管地址:http: ...
- SSM整合(三):Spring4与Mybatis3与SpringMVC整合
源码下载 SSMDemo 上一节整合了Mybatis3与Spring4,接下来整合SpringMVC! 说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目! 本节全部采用 ...
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
- springmvc整合fastjson
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
随机推荐
- Win10系列:C#应用控件基础4
ComboBox控件 ComboBox控件包含一个不可编辑的文本框和一个下拉列表,这个下拉列表是由多个ComboBoxItem子元素组成的.使用ComboBox控件可以节省界面空间,因为ComboBo ...
- Android 应用基础知识和应用组件
应用基础知识 安装到设备后,每个 Android 应用都运行在自己的安全沙箱内: Android 操作系统是一种多用户 Linux 系统,其中的每个应用都是一个不同的用户: 默认情况下,系统会为每个应 ...
- react+dva+antd/antd-mobile
github仓库pc: https://github.com/llcMite/react-dva-antd.git github仓库mobile:https://github.com/llcMite/ ...
- 回溯算法 LEETCODE别人的小结 一八皇后问题
回溯算法实际上是一个类似枚举的搜索尝试过程,主要是在搜索尝试中寻找问题的解,当发现已不满足求解条件时,就回溯返回,尝试别的路径. 回溯法是一种选优搜索法,按选优条件向前搜索,以达到目的.但是当探索到某 ...
- day15-python常用内置模块的使用
在日常的开发工作中,我们要写很多的python代码,如果都写在一个文件中,会导致代码特别难维护,为了拓展代码的可维护性,我们把函写在不同的文件里,这样每个文件包含的文件就比较少,逻辑更加清楚.在pyt ...
- java⑩
1.for循环: for循环语法 for(表达式1;表达式2;表达式3){ 循环体4} 表达式1:初始化变量 只执行一次!表达式2:循环条件 满足条件进入循环体4表达式3:迭代变量 如果循环体 中只有 ...
- mysql 数据迁移
最近线上系统新挂了一次磁盘,需要将系统磁盘下的 mysql 数据目录迁移到 数据盘上. 经过一番考察,mysql在安装时,使用了预编译的二进制tar.gz包.共有两处配置了 datadir属性 /et ...
- Cracking The Coding Interview4.8
//You are given a binary tree in which each node contains a value. Design an algorithm to print all ...
- Linux输入子系统 : 按键驱动
一.Linux input system框架: 1.由输入子系统核心层(input.c),驱动层(gpio_keys.c)和事件处理层(Event Handler)三部份组: 2.主要的三个结构体:i ...
- 深入理解java虚拟机---对象的结构(九)
注意: 我们可以看到的就是InstanceData的数据. 先转载一篇文章作为开头,因为讲的非常详细,我就简单加工下放到这里: 对象结构 在HotSpot虚拟机中,对象在内存中存储的布局可以分为3块区 ...