Spring Boot中的模板引擎选择与配置

简介: Spring Boot中的模板引擎选择与配置

Spring Boot中的模板引擎选择与配置

今天我们来聊聊Spring Boot中的模板引擎选择与配置。模板引擎是生成动态网页的关键组件,在Spring Boot中,我们有多种模板引擎可以选择,如Thymeleaf、FreeMarker和Mustache。本文将介绍这些模板引擎的基本特点,并提供配置示例,帮助你快速上手。

一、模板引擎概述

模板引擎用于将数据与模板结合,生成动态的HTML内容。在Spring Boot中,常用的模板引擎包括:

  • Thymeleaf:功能强大,语法简洁,支持Spring EL(表达式语言),与Spring Boot集成良好。
  • FreeMarker:高度可定制,支持复杂的数据处理,适合生成复杂的动态内容。
  • Mustache:轻量级,语法简单,逻辑与视图分离,适合需要简单模板的应用。

二、Thymeleaf的配置与使用

Thymeleaf是Spring Boot默认推荐的模板引擎,配置简单,功能强大。以下是Thymeleaf的配置与使用示例:

  1. 添加依赖

pom.xml中添加Thymeleaf依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  1. 配置模板路径

application.properties中配置模板路径:

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
  1. 创建Controller

创建一个简单的Controller,返回视图:

package cn.juwatech.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {
   

    @GetMapping("/home")
    public String home(Model model) {
   
        model.addAttribute("message", "欢迎使用Thymeleaf模板引擎!");
        return "home";
    }
}
  1. 创建模板

src/main/resources/templates目录下创建home.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Home</title>
</head>
<body>
    <h1 th:text="${message}">Thymeleaf 模板引擎</h1>
</body>
</html>

访问/home路径即可看到动态生成的内容。

三、FreeMarker的配置与使用

FreeMarker是另一款流行的模板引擎,适合处理复杂的模板需求。以下是FreeMarker的配置与使用示例:

  1. 添加依赖

pom.xml中添加FreeMarker依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
  1. 配置模板路径

application.properties中配置模板路径:

spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.suffix=.ftl
  1. 创建Controller

创建一个简单的Controller,返回视图:

package cn.juwatech.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class FreeMarkerController {
   

    @GetMapping("/freemarker")
    public String freemarker(Model model) {
   
        model.addAttribute("message", "欢迎使用FreeMarker模板引擎!");
        return "freemarker";
    }
}
  1. 创建模板

src/main/resources/templates目录下创建freemarker.ftl

<!DOCTYPE html>
<html>
<head>
    <title>FreeMarker</title>
</head>
<body>
    <h1>${message}</h1>
</body>
</html>

访问/freemarker路径即可看到动态生成的内容。

四、Mustache的配置与使用

Mustache是一款轻量级的模板引擎,适合需要简单模板的应用。以下是Mustache的配置与使用示例:

  1. 添加依赖

pom.xml中添加Mustache依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mustache</artifactId>
</dependency>
  1. 配置模板路径

application.properties中配置模板路径:

spring.mustache.prefix=classpath:/templates/
spring.mustache.suffix=.mustache
  1. 创建Controller

创建一个简单的Controller,返回视图:

package cn.juwatech.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MustacheController {
   

    @GetMapping("/mustache")
    public String mustache(Model model) {
   
        model.addAttribute("message", "欢迎使用Mustache模板引擎!");
        return "mustache";
    }
}
  1. 创建模板

src/main/resources/templates目录下创建mustache.mustache

<!DOCTYPE html>
<html>
<head>
    <title>Mustache</title>
</head>
<body>
    <h1>{
  {message}}</h1>
</body>
</html>

访问/mustache路径即可看到动态生成的内容。

五、模板引擎的选择

选择合适的模板引擎取决于具体的项目需求:

  • 如果需要与Spring Boot紧密集成,推荐使用Thymeleaf。
  • 如果需要高度定制和复杂的数据处理,推荐使用FreeMarker。
  • 如果需要轻量级、简单的模板,推荐使用Mustache。

每种模板引擎都有其优点和适用场景,根据项目的具体需求进行选择和配置,才能更好地发挥它们的优势。

六、总结

本文介绍了Spring Boot中常用的三种模板引擎:Thymeleaf、FreeMarker和Mustache,并提供了详细的配置和使用示例。通过这些示例,大家可以快速上手并在项目中灵活使用不同的模板引擎。

相关文章
|
3月前
|
Java Spring
Spring boot 运行服务jar外配置配置文件方式总结
Spring boot 运行服务jar外配置配置文件方式总结
488 0
|
3月前
|
Java 测试技术 数据库
Spring Boot中的项目属性配置
本节课主要讲解了 Spring Boot 中如何在业务代码中读取相关配置,包括单一配置和多个配置项,在微服务中,这种情况非常常见,往往会有很多其他微服务需要调用,所以封装一个配置类来接收这些配置是个很好的处理方式。除此之外,例如数据库相关的连接参数等等,也可以放到一个配置类中,其他遇到类似的场景,都可以这么处理。最后介绍了开发环境和生产环境配置的快速切换方式,省去了项目部署时,诸多配置信息的修改。
|
19天前
|
XML Java 数据格式
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
116 18
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
|
7天前
|
前端开发 Java Spring
关于spring mvc 的 addPathPatterns 拦截配置常见问题
关于spring mvc 的 addPathPatterns 拦截配置常见问题
|
20天前
|
Java 数据库连接 Maven
Spring基础1——Spring(配置开发版),IOC和DI
spring介绍、入门案例、控制反转IOC、IOC容器、Bean、依赖注入DI
Spring基础1——Spring(配置开发版),IOC和DI
|
1月前
|
IDE Java 开发工具
还在为繁琐的配置头疼吗?一文教你如何用 Spring Boot 快速启动,让开发效率飙升,从此告别加班——打造你的首个轻量级应用!
【9月更文挑战第2天】Spring Boot 是一款基于 Spring 框架的简化开发工具包,采用“约定优于配置”的原则,帮助开发者快速创建独立的生产级应用程序。本文将指导您完成首个 Spring Boot 项目的搭建过程,包括环境配置、项目初始化、添加依赖、编写控制器及运行应用。首先需确保 JDK 版本不低于 8,并安装支持 Spring Boot 的现代 IDE,如 IntelliJ IDEA 或 Eclipse。
86 5
|
2月前
|
Java 微服务 Spring
Spring Cloud全解析:配置中心之解决configserver单点问题
但是如果该configserver挂掉了,那就无法获取最新的配置了,微服务就出现了configserver的单点问题,那么如何避免configserver单点呢?
|
2月前
|
运维 Java Nacos
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
|
2月前
|
Java Spring 开发者
解锁 Spring Boot 自动化配置的黑科技:带你走进一键配置的高效开发新时代,再也不怕繁琐设置!
【8月更文挑战第31天】Spring Boot 的自动化配置机制极大简化了开发流程,使开发者能专注业务逻辑。通过 `@SpringBootApplication` 注解组合,特别是 `@EnableAutoConfiguration`,Spring Boot 可自动激活所需配置。例如,添加 JPA 依赖后,只需在 `application.properties` 配置数据库信息,即可自动完成 JPA 和数据源设置。这一机制基于多种条件注解(如 `@ConditionalOnClass`)实现智能配置。深入理解该机制有助于提升开发效率并更好地解决问题。
49 0
|
2月前
|
Java Spring 开发者
Spring 框架配置属性绑定大比拼:@Value 与 @ConfigurationProperties,谁才是真正的王者?
【8月更文挑战第31天】Spring 框架提供 `@Value` 和 `@ConfigurationProperties` 两种配置属性绑定方式。`@Value` 简单直接,适用于简单场景,但处理复杂配置时略显不足。`@ConfigurationProperties` 则以类级别绑定配置,简化代码并更好组织配置信息。本文通过示例对比两者特点,帮助开发者根据具体需求选择合适的绑定方式,实现高效且易维护的配置管理。
35 0
下一篇
无影云桌面