SpringBoot 整合模板引擎

本文详细介绍了如何在SpringBoot项目中整合Freemarker和Thymeleaf两种模板引擎,包括依赖引入、配置修改及基本使用示例。通过具体步骤演示了如何在控制器中传递数据到模板,并在HTML页面中展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot 整合模板引擎

一 SpringBoot整合freemarker

1.引入freemarker依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

二 SpringBoot整合thymeleaf

1.引入thymeleaf依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

Thymeleaf 支持渲染HTML,因此通常我们使用的页面模板也就是HTML,同时它需要遵循一定的规则:

  1. 比如在spring boot项目中,通常我们将Thymeleaf渲染的页面放在 resources/templates目录下,这样Thymeleaf会默认识别。

  2. 若想要在HTML页面中使用Thymeleaf,需要修改 <htmllang=“en”>为 <htmllang="en"xmlns:th=“http://www.thymeleaf.org”>

  3. 在spring boot项目中 resources/static目录下的文件可通过浏览器直接访问,而 resources/templates下的HTML不能通过浏览器直接访问,而需要 SpringMvc这样的框架映射到那个页面地址。

2.修改 application.properties
#thymeleaf静态资源配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.suffix=.html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
3.Demo

在 main/java目录下新建 RouterController.java

	@RequestMapping("/index")
    public String index(ModelMap map) {
        map.addAttribute("name", "thymeleaf-imooc");
        return "index";
    }

这个 return"index"中 index是 templates根目录下的 index.html,如果是 templates/common/main.html页面,就应该 return"/common/main.html"

在 main/resources/templates目录下新建 index.html

4.常用表达式
变量表达式

变量表达式级即OGNL表达式或Spring EL表达式(在Spring术语中也叫做model attributes),例如:

${user.username}

1.修改 RouterController.java

	@RequestMapping("/index")
    public String index(ModelMap map) {
        map.addAttribute("name", "thymeleaf-imooc");
        return "thymeleaf/index";
    }

2.修改 index.html

<body>
Thymeleaf模板引擎
<h1 th:text="${name}">hello world~~~~~~~</h1>
</body>
选择*号表达式

如果 model.addAttribute()的是一个对象而不是字符串,就可以用 *表达式分别获取 model对象中的元素,如:

`*{...}`表达式:
<b th:object="${user}">
<span th:text="*{username}"></span>
<span th:text="*{password}"></span>
</b>

如果页面需要不在 th:object包裹下获取对象中的元素,可以使用如下:

<span th:text="${user.username}"></span>
URL表达式

例如:

<script th:src="@{/css/base.css}"></script>

会自动引入 resources/statis/css/下的 base.css文件,这和JSP页面中使用:

<script src="${pageContext.request.contextPath}/css/base.css"></script>

是一个道理。

同理:

<link rel="stylesheet" th:href="@{/css/base.css}"/>

表达式支持的语法

字面(Literals)

文本文字(Text literals): ‘one text’, ‘Another one!’,…
数字文本(Number literals): 0, 34, 3.0, 12.3,…
布尔文本(Boolean literals): true, false
空(Null literal): null
文字标记(Literal tokens): one, sometext, main,…
文本操作(Text operations)

字符串连接(String concatenation): +
文本替换(Literal substitutions): |The name is ${name}|
算术运算(Arithmetic operations)

二元运算符(Binary operators): +, -, *, /, %
减号(单目运算符)Minus sign (unary operator): -
布尔操作(Boolean operations)

二元运算符(Binary operators):and, or
布尔否定(一元运算符)Boolean negation (unary operator):!, not
比较和等价(Comparisons and equality)

比较(Comparators): >, <, >=, <= (gt, lt, ge, le)
等值运算符(Equality operators):==, != (eq, ne)
条件运算符(Conditional operators)

If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)

所有这些特征可以被组合并嵌套:

'User is of type '+(${user.isAdmin()}?'Administrator': (${user.type} ?:'Unknown'))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值