SpringBoot整合Thymeleaf

简介:

  在目前的企业级应用开发中 前后端分离是趋势,但是视图层技术还占有一席之地, Spring Boot 对视图层技术提供了很好的支持,官方推荐使用的模板引擎是 Thymeleaf 不过像 FreeMarker 也支持, JSP 技术在这里并不推荐使用。

  Thymeleaf 是新一代 Java 模板引擎,类似于 Velocity、FreeMarker 等传统 Java 模板引擎。与传统 Java 模板引擎不同的是 Thymeleaf 支持 HTML 原型,既可 以让前端工程师在浏览器中直接打 开查看样式,也可以让后端工程师结合真实数据查看显示效果。 同时, Spring Boot 提供了 Thymeleaf 自动 配置解决方案,因此Spring Boot 中使用 Thymeleaf 常方便。

1.引入依赖:

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

2.application.properties

#是否开启缓存,开发时可设置为false,默认为true
spring.thymeleaf.cache=true
#是否检查模板是否存在,默认为true
spring.thymeleaf.check-template=true
#是否检查模板位置是否存在,默认为true
spring.thymeleaf.check-template-location=true
#模板文件编码
spring.thymeleaf.encoding=UTF-8
#模板文件位置
spring.thymeleaf.prefix=classpath:/templates/
#Content-Type配置
spring.thymeleaf.servlet.content-type=text/html
#模板文件后缀
spring.thymeleaf.suffix=.html

3.创建实体类和controller类

public class Book {
    private Integer id;
    private String name;
    private String author;
    //省略getter/setter
}
@Controller
public class BookController {
    @GetMapping("/books")
    public ModelAndView books() {
        List<Book> books = new ArrayList<>();
        Book b1 = new Book();
        b1.setId(1);
        b1.setAuthor("罗贯中");
        b1.setName("三国演义");
        Book b2 = new Book();
        b2.setId(2);
        b2.setAuthor("曹雪芹");
        b2.setName("红楼梦");
        books.add(b1);
        books.add(b2);
        ModelAndView mv = new ModelAndView();
        mv.addObject("books", books);
        mv.setViewName("books");
        return mv;
    }
}

4.html文件:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>图书列表</title>
</head>
<body>
<table border="1">
    <tr>
        <td>图书编号</td>
        <td>图书名称</td>
        <td>图书作者</td>
    </tr>
    <tr th:each="book:${books}">
        <td th:text="${book.id}"></td>
        <td th:text="${book.name}"></td>
        <td th:text="${book.author}"></td>
    </tr>
</table>
</body>
</html>

5.结果:

原文地址:https://www.cnblogs.com/crazy-lc/p/12312157.html

时间: 2024-08-30 17:55:55

SpringBoot整合Thymeleaf的相关文章

Spring Boot入门系列六( SpringBoot 整合thymeleaf)

SpringBoot 整合thymeleaf 一.什么是Thymeleaf模板 Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎.类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎.与其它模板引擎相比,Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用.它的功能特性如下: @Controller中的方法可以直接返回模板名称,接下来Thyme

三、SpringBoot整合Thymeleaf

三.SpringBoot整合Thymeleaf As well as REST web services, you can also use Spring MVC to serve dynamic HTML content. Spring MVC supports a variety of templating technologies, including Thymeleaf, FreeMarker, and JSPs. Also, many other templating engines

SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图

在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: Thymeleaf FreeMarker Velocity Groovy Mustache Spring Boot 推荐使用这些模板引擎来代替 Jsp,Thymeleaf 只是其中一种,下面我们来简单聊聊Thymeleaf及实践一下如何整合Spring Boot和Thymeleaf. 1.Thyme

SpringBoot整合Thymeleaf(三)

? Thymeleaf是SpringBoot官方推荐的用来渲染页面的一个模板,如果你对模板和模板引擎没什么概念的话,可以简单理解为Thymeleaf是一个高级简洁的JSP.如果学过MVC设计模式,那么Thymeleaf就是视图层(view)的主要核心内容. 为什么要整合Thymeleaf SpringBoot在内置Tomcat服务器,并且以Jar包方式运行,传统JSP页面不在适合这种模式开发 使用Thymeleaf有助于前后端协作,因为它在无网络环境下也可以运行,前端开发者便于在静态页面查看页面

springboot整合Thymeleaf模板引擎

引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>nekohtml</groupId&g

SpringBoot学习9:springboot整合thymeleaf

1.创建maven项目,添加项目所需依赖 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <de

springboot 整合 thymeleaf的问题

org.thymeleaf.exceptions.TemplateInputException org.thymeleaf.exceptions.TemplateInputException: Error resolving template "Hello", template might not exist or might not be accessible by any of the configured Template Resolvers 因为springboot 是2.0版

springBoot整合thymeleaf时th:onclick事件

今天按照 <JavaEE开发的颠覆者: Spring Boot实战> 学习spring boot的时候在编写7_2时thymeleaf传参的时候发生500错误,通过查找各方面资料,发现书上的springboot版本(1.3.0)和我用的版本(2.1.7)不一样引起的 以下处理方法: 1.   将springboot版本改成2.0.5 2.   3.x版本的thymeleaf为了防止注入攻击,升级了安全机制 解决 修改前 <button th:onclick="'javascri

springboot整合thymeleaf——引用静态资源

原html src="/css/index.css" thymeleaf中,th:src="@{/css/index.css}" 原文地址:https://www.cnblogs.com/fangwr/p/11939243.html