`

Spring MVC / Spring Boot中使用Thymeleaf模板引擎

 
阅读更多

新一代Java模板引擎Thymeleaf一定让你惊叹于Thymeleaf的强大,但是真正在Web应用结合Web特性使用模板引擎,还需要进行一定的配置和学习。

Thymeleaf于Spring集成

Thymeleaf除了基本的模板引擎,还提供了一套Spring集成技术使得在Spring MVC中能够使用它完全替代JSP作为模板引擎,它的功能特性如下:

  • Spring MVC中@Controller中的方法可以直接返回模板名称,接下来Thymeleaf模板引擎会自动进行渲染
  • 模板中的表达式支持Spring表达式语言(Spring EL)
  • 表单支持,并兼容Spring MVC的数据绑定与验证机制
  • 国际化支持

如果你还不了解Thymeleaf,请一定先阅读新一代Java模板引擎Thymeleaf

配置TemplateEngine

<bean id="templateResolver"
       class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
  <property name="prefix" value="/WEB-INF/templates/" />
  <property name="suffix" value=".html" />
  <property name="templateMode" value="HTML5" />
</bean>

<bean id="templateEngine"
      class="org.thymeleaf.spring4.SpringTemplateEngine">
  <property name="templateResolver" ref="templateResolver" />
</bean>

上述配置的TemplateEngine从/WEB-INF/templates/目录中读取文件夹,默认的后缀名是.html,所以在渲染模板时只需要提供模板的名字(例如index.html可以省略为index),TemplateEngine就可以找到对应的模板内容。

为了能够方便的让@Controller进行渲染(类似JSP),例如:

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index() {
        return "index";
    }
}

还需要配置Spring中的ViewResolver

<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
  <property name="templateEngine" ref="templateEngine" />
</bean>

@Controller

Spring MVC中@Controller用于处理HTTP请求并返回内容到浏览器。在渲染模板前,ThymeleafViewResolver会自动把当前的Model加入到Context中:

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index(Model model) {
        model.addAttribute("list", Lists.newArrayList("a", "b", "c"));
        return "index";
    }
}

这样在index模板中可以访问表达式${list}得到`["a", "b", "c"]的值。

表单

Command对象用来在Spring MVC中绑定表单与后端对象,Thymeleaf提供的th:object属性可以用来指定Command对象:

<form action="#" th:action="@{/}" th:object="${command}" method="post">
  <label>Title</label>
  <input type="text" th:field="*{title}"/>
  <label>Text</label>
  <input type="text" th:field="*{text}"/>
  <br/>
  <input type="submit" value="Add"/>
</form>

<div>
  <div th:each="entry: ${entries}">
    <h2 th:text="${entry.title}">Title</h2>
    <p th:text="${entry.text}">Text</p>
  </div>
</div>

指定th:object属性后,各个<input>中还需要指定th:field,这与后端绑定对象的字段名要一致。在@Controller中的方法如下:

@RequestMapping(value = "/", method = GET)
public String index(Model model) {
    model.addAttribute("entries", getAll());
    model.addAttribute("command", new Entry());
    return "index";
}

@RequestMapping(value = "/", method = POST)
public String post(Entry entry) {
    add(entry.title, entry.text);

    return "redirect:/";
}

post()方法的参数Entry entry是根据HTTP请求的输入titletext值自动进行的绑定。

在Spring Boot中使用Thymeleaf

Spring Boot能够简化Spring应用配置、加速开发,对于Thymeleaf模板引擎提供了内置支持,在Spring Boot应用中只需要引入:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

所有Thymeleaf的相关依赖都会被加载到类路径中,更重要的是,上文中所有TemplateEngineThymeleafViewResolver等bean都在应用启动后被自动实例化,默认情况下模板的目录位于src/main/resources/templates/文件夹中,所以开发者只需要在这个文件夹中添加模板文件即可。

如果需要改变一些配置,可以在application.properties中写入:

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html # ;charset=<encoding> is added

改变这些配置的值,也就会自动改变Spring Boot帮助我们实例化的bean的配置。

https://www.tianmaying.com/tutorial/spring-mvc-thymeleaf

分享到:
评论

相关推荐

    《Spring Boot 2+Thymeleaf企业应用实战》_杨恩雄.pdf

    《Spring Boot 2+Thymeleaf企业应用实战》是杨恩雄编著的一本面向Java Web开发的技术书籍,这本书籍主要介绍了Spring Boot框架及其与Thymeleaf模板引擎的结合使用。内容覆盖了从Spring Boot和Spring MVC基础,到...

    Gradle/Spring Boot/Thymeleaf

    这个应用可能包含了基本的Gradle构建配置,Spring Boot启动类,以及使用Thymeleaf模板引擎的视图文件。开发者可能已经配置了Spring Security来保护应用的某些部分。通过分析`MyFirstApp`的源代码和相关配置,我们...

    gradle+springboot+mysql+mybatis+thymeleaf开发的web实例

    6. **配置Thymeleaf**:在Spring Boot项目中启用Thymeleaf模板引擎,然后在`src/main/resources/templates`目录下创建HTML模板文件。Thymeleaf允许我们在HTML中使用表达式语言来动态渲染数据。 7. **编写Controller...

    spring_boot使用的 模版引擎 thymeleaf3.0

    在Spring Boot项目中使用Thymeleaf作为模板引擎是目前Web开发中常见的实践之一,而Thymeleaf 3.0作为最新版本,已经广泛地集成在Spring MVC应用中,其功能和使用方式都成为开发人员必须掌握的技术要点。Thymeleaf是...

    springboot用thymeleaf模板引擎

    SpringBoot与Thymeleaf模板引擎的整合是现代Java Web开发中的常见实践,它为开发者提供了便捷的MVC(Model-View-Controller)框架支持,实现了动态HTML页面的渲染。Thymeleaf是一款强大的服务器端模板引擎,尤其适用...

    Spring Boot 2.0 + Thymeleaf模板+简单增删改查分页

    在本项目中,我们主要探讨的是如何利用Spring Boot 2.0.4版本与Thymeleaf模板引擎来实现一个包含基本CRUD操作(创建、读取、更新、删除)和分页功能的Web应用程序。Spring Boot是Spring框架的一个简化版,它提供了...

    spring boot+thymeleaf项目实战

    7. **错误处理和模板布局**:学习如何使用Thymeleaf处理Spring MVC的模型错误,并创建可重用的布局模板,以提高代码复用性。 在“favorites-web-master”这个项目中,你可以期待看到一个完整的Web应用结构,包含...

    spring-boot idea Thymeleaf模板引擎 增删改查

    在这个主题中,我们将深入探讨如何使用Spring Boot与Thymeleaf模板引擎进行Web开发中的增删改查操作。 Thymeleaf是一个现代的服务器端HTML模板引擎,它支持HTML5, XML, SVG 和 JavaScript。它的一大特点是在静态...

    Springboot从入门到实战-04-Thymeleaf常用语法(四)

    Spring Boot 项目中 ...在 Spring Boot 项目中,Thymeleaf 模板引擎可以与 Spring MVC 框架集成使用,提供了强大的模板引擎功能。Thymeleaf 模板引擎的应用场景非常广泛,可以应用于各种 Web 应用程序的开发中。

    Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图的方法

    Spring Boot与Kotlin结合使用Thymeleaf模板引擎渲染Web视图是一种常见的做法,尤其在构建现代化的MVC应用程序中。Thymeleaf是一个强大的、灵活的模板引擎,支持XML、XHTML和HTML5,其设计目标是提供一种在前端展示...

    spring boot+spring mvc+mybatis+thymeleaf整合开发学生成绩信息管理系统

    项目描述 学生成绩管理系统,有三...spring boot+spring mvc+mybatis+layui+jquery+thymeleaf http://localhost:8080/Sys/loginView 管理员账号 admin admin 老师登录 2020031920 111111 学生账号登录 20200319 111111

    spring boot+hibernate+spring mvc+thymeleaf 实例

    spring boot+hibernate+spring mvc+thymeleaf 实例,下载导入即可运行,可独立部署到独立tomcat,可内置tomcat运行,配置都在,数据库mysql直接导入脚本即可,可加我扣扣410987712一起学习

    springboot-thymeleaf

    在Web开发中,Spring Boot推荐使用模板引擎来处理视图,其中Thymeleaf因其易于学习、语法直观且支持服务器端和静态原型开发等特点,成为了常见选择。 Thymeleaf是一种用于Web和非Web环境的现代服务器端Java模板引擎...

    springboot整合thymeleaf模板

    SpringBoot整合Thymeleaf模板是一项常见的Web开发任务,它结合了SpringBoot的便捷性和Thymeleaf的动态模板引擎,使得开发人员可以快速构建功能丰富的Web应用。下面将详细介绍这个过程及其涉及的关键知识点。 首先,...

    为Spring mvc、Thymelaaf模板引擎、H2数据库、Lombok和消息传递Spring Boot应用程序

    本文将深入探讨如何利用Spring MVC、Thymeleaf模板引擎、H2内存数据库、Lombok以及Spring Boot中的消息传递功能来创建一个强大的应用程序。 **Spring MVC**: Spring MVC是Spring框架的一部分,它提供了一个用于...

    Spring boot整合mybaties+thymeleaf实现基础

    Thymeleaf 是一个现代服务器端 Java 模板引擎,主要用于 Web 开发,它可以与 Spring MVC 等其他框架无缝集成。Thymeleaf 的主要特点是能够在浏览器中直接解析 HTML 页面,提供了一种静态原型到动态网页的平滑过渡...

    springboot整合thymeleaf+maven实现异常处理页面

    通过这个整合,我们不仅了解了如何在 SpringBoot 中使用 Thymeleaf 和 Maven,还学习了如何实现优雅的异常处理,提高了应用的稳定性和用户体验。同时,这个案例也展示了如何在实际开发中结合多种技术,解决具体问题...

    spring boot thymeleaf mybatis

    Spring Boot、Thymeleaf 和 MyBatis 是三个在Java Web开发中广泛使用的开源框架,它们结合使用可以构建高效、简洁的Web应用程序。下面将详细解释这三个技术以及它们如何协同工作。 1. Spring Boot: Spring Boot是...

    spring boot+thymeleaf+bootstrap 简单实现后台管理系统界面(看评论酌情下载)

    Thymeleaf 在Spring Boot 中的整合,使得开发者能够方便地进行视图层的渲染,同时提供了与Spring MVC的无缝集成。 Bootstrap 是一个流行的前端开发框架,用于构建响应式和移动优先的网站。它包含了一系列预先设计的...

Global site tag (gtag.js) - Google Analytics