iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > html >SpringBoot整合Thymeleaf视图的方法是什么
  • 576
分享到

SpringBoot整合Thymeleaf视图的方法是什么

2024-04-02 19:04:59 576人浏览 薄情痞子
摘要

这篇文章主要介绍了SpringBoot整合Thymeleaf视图的方法是什么的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇springBoot整合Thymeleaf视图的方法是

这篇文章主要介绍了SpringBoot整合Thymeleaf视图的方法是什么的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇springBoot整合Thymeleaf视图的方法是什么文章都会有所收获,下面我们一起来看看吧。

Thymeleaf视图介绍

先看下官网的介绍:
==Thymeleaf是适用于WEB和独立环境的现代服务器端Java模板引擎。
Thymeleaf的主要目标是为您的开发工作流程带来优雅的自然模板 -html可以在浏览器中正确显示,也可以作为静态原型工作,从而可以在开发团队中加强协作。
Thymeleaf拥有适用于Spring Framework的模块,与您喜欢的工具的大量集成以及插入您自己的功能的能力,对于现代HTML5 JVM web开发而言,Thymeleaf是理想的选择。==
在SpringBoot中,SpringBoot对Thymeleaf提供了良好的支持,同时也提供了自动化配置,因此在SpringBoot中使用Thymeleaf非常快捷方便。

创建SpringBoot项目

创建方法建议使用idea快速创建SpringBoot项目,并选择web、Thymeleaf依赖
创建完成后,IDEA自动在pom中加入了web和Thymeleaf依赖管理,pom.xml:

<dependencies>
        <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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

配置Thymeleaf

SpringBoot为Thymeleaf提供了自动化配置类ThymeleafAutoConfiguration,源码

@Configuration
@EnableConfigurationProperties({ThymeleafProperties.class})
@ConditionalOnClass({TemplateMode.class, SpringTemplateEngine.class})
@AutoConfigureAfter({WebmvcAutoConfiguration.class, WebFluxAutoConfiguration.class})
public class ThymeleafAutoConfiguration {...}

可以看出相关的配置信息是从ThymeleafProperties类中获得的,进一步查看ThymeleafProperties的源码:

@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENcoding;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML";
    //省略
}

从该配置可以看出默认的Thymeleaf存放位置是classpath:/templates/,即resources/templates/下,刚刚我们使用IDEA创建项目时,已经自动生成了该目录。
我们如果需要对Thymeleaf的配置进行更改,可直接在application.properties中配置:

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

编写Demo

1、新建User和UserController:
User.java:

package com.Gongsir.springboot02.pojo;

public class User {
    private String name;
    private String major;
    private String grade;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }
}

UserController.java:

@Controller
public class UserController {
    @GetMapping(path = "/users")
    public ModelAndView getUsers(){
        List<User> list = new ArrayList<>();
        User u1 = new User();
        u1.setName("龚涛");
        u1.setMajor("计算机");
        u1.setGrade("2017");
        list.add(u1);
        User u2 = new User();
        u2.setName("李诗雅");
        u2.setMajor("网络工程");
        u2.setGrade("2017");
        list.add(u2);
        //视图模板文件的名字,需在template目录下创建同名模板文件
        ModelAndView mv = new ModelAndView("users");
        mv.addObject("users",list);
        return mv;
    }
}

2、在模板目录下新建users.html模板文件,显示数据:

<!DOCTYPE html>
<html lang="en" xmlns:th="Http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户列表</title>
</head>
<body>
    <table border="1px sold black">
        <tr>
            <td>姓名</td>
            <td>专业</td>
            <td>年级</td>
        </tr>
        <tr th:each="user:${users}">
            <td th:text="${user.name}"></td>
            <td th:text="${user.major}"></td>
            <td th:text="${user.grade}"></td>
        </tr>
    </table>
</body>
</html>

3、启动项目,访问http://localhost:8080/users

关于“SpringBoot整合Thymeleaf视图的方法是什么”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“SpringBoot整合Thymeleaf视图的方法是什么”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网html频道。

--结束END--

本文标题: SpringBoot整合Thymeleaf视图的方法是什么

本文链接: https://www.lsjlt.com/news/88491.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
推荐阅读
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作