SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用
1. freemarker引擎的使用
如果你使用的是idea或者eclipse中安装了sts插件,那么在新建项目时就可以直接指定试图模板
如图:

勾选freeMarker,此时springboot项目中就会自动引入freemarker的依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
如果不是idea且eclipse也没有插件,那么就需要手动添加这个依赖到pom文件中。
springboot存放模板和静态文件目录结构如下:

templates中存放模板文件,static中存放一些静态文件,如图片,css,js等
templates在springboot中默认为模板根目录,static默认为静态文件根目录,所以我们在写路径的时候不用将这两个目录路径写到访问目录中。
不需要任何配置,只需要一个controller接口,就可以直接使用controller和模板直接进行交互,直接上代码
IndexController接口:
package com.wangx.boot.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.Map; @Controller
@RequestMapping("/index")
public class IndexController { @RequestMapping("/index")
public String hello(Map<String,Object> map){
//传递数据到freemaker模板中
map.put("name", "[Angel -- 守护天使]");
return "index"; }
}
freemarker模板文件的默认后缀名为ftl.
index.ftl
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<p>
<!--获取后台传过来的数据-->
<p>${name}</p>
</p>
</body>
</html>
当请求返回index时,springboot会根据这个路径到templates目录下自动匹配这个模板引擎,这样就完成了后台到模板的整合和数据交互。
结果如下:

如果想要访问静态资源时,如图片,直接写上绝对路径<img src="/1.jpg">即可(注意在Controller方法返回值中尽量写相对路径,即不要带/,否则在linux环境下中可能会出错)。
2. thymeleaf
thymeleaf的使用与freemarker很相似,简单的使用只需要将示例1中的ftl改为html文件,取值方式改为thymeleaf即可,这里只展示thymeleaf的html代码,其他的代码跟freemarker一样,只是在创建项目时选择thymeleaf或在pom文件中添加thymeleaf依赖即可。
index.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello World!</title>
</head>
<body>
<p th:text="${name}">
<img src="1.jpg">
</p>
</body>
</html>
启动项目,访问http://localhost:8080/index/index,结果与1相同。
这里只是简单的展示怎么整合springboot和模板引擎的整合,模板引擎的具体使用方式请参照官网。
freemarker官方文档:https://freemarker.apache.org/docs/
thymeleaf:官方文档:https://www.thymeleaf.org/
在springboot官方推荐使用的是thymeleaf模板,因为现在的趋势都是前后端分离的架构,所以使用thymeleaf耦合性会更低。
SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用的更多相关文章
- SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用
1. activemq 首先引入依赖 pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId& ...
- SpringBoot学习笔记(10)-----SpringBoot中使用Redis/Mongodb和缓存Ehcache缓存和redis缓存
1. 使用Redis 在使用redis之前,首先要保证安装或有redis的服务器,接下就是引入redis依赖. pom.xml文件如下 <dependency> <groupId&g ...
- SpringBoot学习笔记(9)----SpringBoot中使用关系型数据库以及事务处理
在实际的运用开发中,跟数据库之间的交互是必不可少的,SpringBoot也提供了两种跟数据库交互的方式. 1. 使用JdbcTemplate 在SpringBoot中提供了JdbcTemplate模板 ...
- SpringBoot学习笔记(6)----SpringBoot中使用Servlet,Filter,Listener的三种方式
在一般的运用开发中Controller已经大部分都能够实现了,但是也不排除需要自己实现Servlet,Filter,Listener的方式,SpringBoot提供了三种实现方式. 1. 使用Bean ...
- SpringBoot学习笔记(5)----SpringBoot中异常处理的三种方法
对于异常的处理,Spring Boot中提供默认的一个异常处理界面,如下图: 但是在实际的运用开发中,这样的页面显然是不友好的,Spring Boot也提供了自定义异常处理的方式,如下总结三种一场处理 ...
- springboot学习笔记-5 springboot整合shiro
shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/ 它提供了很方便的权限认证和登录的功能. 而springboot作为一个开源框架,必然提供了和sh ...
- SpringBoot学习笔记(6) SpringBoot数据缓存Cache [Guava和Redis实现]
https://blog.csdn.net/a67474506/article/details/52608855 Spring定义了org.springframework.cache.CacheMan ...
- springboot学习笔记-6 springboot整合RabbitMQ
一 RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件.这些软件有很多,包括ActiveMQ(apache公司的),RocketMQ(阿 ...
- 【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)
http://blog.csdn.net/a67474506/article/details/61640548 Dubbo是什么东西我这里就不详细介绍了,自己可以去谷歌 SpringBoot整合Dub ...
随机推荐
- 洛谷P4012 深海机器人问题(费用流)
题目描述 深海资源考察探险队的潜艇将到达深海的海底进行科学考察. 潜艇内有多个深海机器人.潜艇到达深海海底后,深海机器人将离开潜艇向预定目标移动. 深海机器人在移动中还必须沿途采集海底生物标本.沿途生 ...
- DevExpress Report 打印提示one or more margins are set outside the printable area of the page 问题解决
DevExpress Report Print的时候,出现这样的问题:one or more margins are set outside the printable area of the pa ...
- MySQL构造测试数据
构造测试数据(笛卡尔积,6 次100 万) create table t1(id int, val varchar(80)); set @i := 0;create table tmp as sele ...
- Codeforces Round #284 (Div. 2) A
解题思路:给出 n个电影的精彩时段(a[i],b[i]),和每次可以跳过的时间x,问要看完所有的精彩时刻,至少需要看多长时间的电影. 因为要时间最少,所有除了精彩时刻的电影则能跳过就跳过(用取余来算) ...
- JS取出特定字符前后的字符串,针对一串字符里面的单个字符前后的字符串
//针对一串自负里面的单个字符前后的字符串<!doctype html> <html> <head> <meta charset="utf-8&qu ...
- Build rpm example:zram
rpmbuild #ll zram-1.0.0 total 32 -rw-r--r-- 1 root root 948 Aug 21 16:44 Makefile -rw-r--r-- 1 root ...
- php中mysqli 处理查询结果集总结
在PHP开发中,我们经常会与数据库打交道.我们都知道,一般的数据处理操作流程为 接收表单数据 数据入库 //连接数据库 $link = mysqli_connect("my_host&quo ...
- [SCSS] Pure CSS for multiline truncation with ellipsis
1. Pure CSS 2. Responsive 3. No need to recalculate on resize or font’s load event 4. Cross browser
- poj2528 Mayor's posters(线段树,离散化)
离散化的思想: 对于这样的数据 (3,10000). (9,1000000). (5.100000), (1,1000). (7,1000000) 我们能够将其处理为 (2,7). (5,9). (3 ...
- DevExpress TreeList控件的复选框
作者:jiankunking 出处:http://blog.csdn.net/jiankunking TreeList控件能够同一时候显示树结构和其它数据列,即在一个列上建立父子关系展开或收缩,同一时 ...