thymeleaf模板引擎入门
ThymeLeaf是什么
Thymeleaf是一个用于服务器端的java模板引擎,它使用简单但功能强大,目前可以处理的模板类型包括:HTML、XML、TEXT、JavaScript、CSS等。
搭建thymeleaf开发环境
首先创建一个Maven web项目,pom文件依赖信息如下:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.7.RELEASE</version>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
可见,thymeleaf的依赖并不复杂,它只需要一个库文件。
TemplateEngin、ITemplateResolver和IContext
这三个组件构成thymeleaf的核心:
- ITemplateResolver用于设置模板引擎,例如模板的存放目录,模板的后缀,是否开启模板缓存等等。
- TemplateEngine用于解析模板。
- IContext用于保存模板中需要的一些变量。例如要把变量传递到模板中,就可以先把变量放入IContext的实现类中,然后在模板中获取该变量的值。
一个简单的thymeleaf使用例子
为了让程序代码尽可能简单,这里只使用最基本的servlet处理web请求。
创建Servlet
servlet代码如下,它只是一个简单的servlet,没有什么功能:
@WebServlet(urlPatterns="/index")
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L; @Override
public void init() throws ServletException {
super.init();
} @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
引入模板引擎属性
要使模板引擎可以工作,至少需要创建TemplateEngine类和ITemplateResolver接口的实例,因此在刚才创建的servlet中创建两个类属性, 如下:
@WebServlet(urlPatterns="/index")
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L; TemplateEngine templateEngine;
ServletContextTemplateResolver templateResolver; //其他servlet方法
}
初始化模板引擎
这一步可以在servlet的init方法中初始化上面加入的两个属性。一般情况下,模板解析器有一些默认值,例如它默认以html方式解析模板。但是,模板的保存路径和后缀是空的,这两个必须由我们自己设置。
@Override
public void init() throws ServletException {
super.init(); templateEngine = new TemplateEngine();
templateResolver = new ServletContextTemplateResolver(getServletContext());
templateResolver.setPrefix("/WEB-INF/templates"); //模板位置
templateResolver.setSuffix(".html"); //模板扩展名 templateEngine.setTemplateResolver(templateResolver);
}
其中prefix就是模板保存的路径,suffix就是模板的扩展名(后缀)。
使用WebContext
WebContext是IContext的其中一个实现类,它的基本作用是保存变量。
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { WebContext context = new WebContext(request, response, getServletContext()); //添加变量message到context
context.setVariable("message", "hello thymeleaf"); //解析模板并显示到浏览器
templateEngine.process("home", context, response.getWriter());
}
当请求到来的时候,我们创建一个上下文对象用于保存变量,将来可以在模板中获取变量的值。
TemplateEngine的process方法用于解析模板并利用当前response对象的writer把模板输出到浏览器。
整个过程是非常简单清晰的。
在模板中获取变量的值
home.html文件内容:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3 th:text="${message}">this is a greeting</h3>
</body>
</html>
代码中的核心是th:text标签和${message}占位符,它们用于把从上下文中获取到的变量值替换掉当前标签的文本,这里是:this is a greeting
完整代码:
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.sharpcode</groupId>
<artifactId>learnthymeleaf</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>learnthymeleaf Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.7.RELEASE</version>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies> <build>
<finalName>learnthymeleaf</finalName> <plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build> </project>
IndexServlet
@WebServlet(urlPatterns = "/home")
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L; TemplateEngine templateEngine;
ServletContextTemplateResolver templateResolver; @Override
public void init() throws ServletException {
super.init(); templateEngine = new TemplateEngine(); templateResolver = new ServletContextTemplateResolver(getServletContext());
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html"); templateEngine.setTemplateResolver(templateResolver);
} @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { WebContext context = new WebContext(request, response, getServletContext()); // 添加变量message到context
context.setVariable("message", "hello thymeleaf"); // 解析模板并显示到浏览器
templateEngine.process("home", context, response.getWriter());
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
thymeleaf模板引擎入门的更多相关文章
- SpringBoot入门篇--使用Thymeleaf模板引擎进行页面的渲染
在做WEB开发的时候,我们不可避免的就是在前端页面之间进行跳转,中间进行数据的查询等等操作.我们在使用SpringBoot之前包括我在内其实大部分都是用的是JSP页面,可以说使用的已经很熟悉.但是我们 ...
- Thymeleaf 模板引擎简介
目录 Thymeleaf 模板引擎 官方文档下载 Hello World 新建应用 后台控制器 前端页面 浏览器访问测试 Thymeleaf 模板引擎1.Thymeleaf 是 Web 和独立环境的现 ...
- SpringBoot--- Shiro(拦截,认证)、Thymeleaf(模板引擎)
SpringBoot--- Shiro(拦截,认证).Thymeleaf(模板引擎) 环境 IDEA :2020.1 SpringBoot: 2.3.3 Java : 8 版本依赖: shiro- ...
- 狂神说SpringBoot11:Thymeleaf模板引擎
狂神说SpringBoot系列连载课程,通俗易懂,基于SpringBoot2.2.5版本,欢迎各位狂粉转发关注学习. 微信公众号:狂神说(首发) Bilibili:狂神说Java(视频) 未经作 ...
- (二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用
一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- 三、thymeleaf模板引擎构建前台html, 后台使用 ModelAndView 和 Model 模型
项目源码:https://github.com/y369q369/springBoot.git -> thymeleaf 私聊QQ: 1486866853 1.pom.xml中 ...
- SpringBoot使用thymeleaf模板引擎
(1).添加pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- Spring Boot 2.0 整合Thymeleaf 模板引擎
本节将和大家一起实战Spring Boot 2.0 和thymeleaf 模板引擎 1. 创建项目 2. 使用Spring Initlizr 快速创建Spring Boot 应用程序 3. 填写项目配 ...
随机推荐
- 前端解读Webview
作为盛行已久的开发方式,Hybrid的相关介绍已经是相当普遍了.不过看到博客园里基本上都是从android或者ios的角度来讲解的,对于h5的前端来说看起来只能是一直半解.感觉有必要从前端的角度来理解 ...
- vsftpd配置虚拟用户
#安装vsftpd yum -y install vsftpd #创建本地ftp账户 groupadd ftpuser useradd -g ftpuser -s /sbin/nologin ftpu ...
- Unreal Engine 4 Radiant UI 入门教程(零)在场景中摆放网页
相关的学习资源: https://forums.unrealengine.com/showthread.php?12097-PLUGIN-RadiantUI-SDK-UIs-HUDs-Interact ...
- Unity3D 物体移动到指定点
transform.position=Vector3.MoveTowards(transform.position , Target.position, speed * Time.deltaTime) ...
- 实现一个栈类,类似STL中的栈
1.思路讲解 stack集合类是一个简单的堆栈的实现. 这里有两个模板参数,T和size,T用于指定堆栈中的元素类型,my_size用于表示堆栈中项数的最大值. 类中添加方法isempty.isful ...
- 如何在web项目中添加javamelody monitoring 监控。
1.在工程的maven pom中添加依赖javamelody-core <!-- monitoring监控 --><!-- https://mvnrepository.com/art ...
- Storm笔记——技术点汇总
目录 概况 手工搭建集群 引言 安装Python 配置文件 启动与测试 应用部署 参数配置 Storm命令 原理 Storm架构 Storm组件 Stream Grouping 守护进程容错性(Dae ...
- 【NOI模拟】谈笑风生(主席树)
题目描述 设 T 为一棵有根树,我们做如下的定义: 设 a 和 b 为 T 中的两个不同节点.如果 a 是 b 的祖先,那么称 “ a 比 b 不知道高明到哪里去了 ” . 设 a 和 b 为 T 中 ...
- 【Spring】关于SpringMvc监听的知识点
一,在使用Spring系列框架时,我们需要在Web.xml配置Spring的监听:ContextLoaderListener ContextLoaderListener的作用就是,在Web容器初始化时 ...
- (转)Linux修改SSH登录欢迎语
场景:感觉这样做挺个性的,做个记录! 1 Linux修改SSH的欢迎语 众所周知,Linux系统并没有像Windows一样自带远程桌面连接,虽然可以通过后期安装VNC之类的软件来弥补这个缺点,但用了L ...