SpringBoot整合Thymeleaf(三)

? Thymeleaf是SpringBoot官方推荐的用来渲染页面的一个模板,如果你对模板和模板引擎没什么概念的话,可以简单理解为Thymeleaf是一个高级简洁的JSP。如果学过MVC设计模式,那么Thymeleaf就是视图层(view)的主要核心内容。

为什么要整合Thymeleaf

  1. SpringBoot在内置Tomcat服务器,并且以Jar包方式运行,传统JSP页面不在适合这种模式开发
  2. 使用Thymeleaf有助于前后端协作,因为它在无网络环境下也可以运行,前端开发者便于在静态页面查看页面效果,后台开发者也便于在服务器查看实时的数据交互。
  3. 实现严格的mvc设计模式,不用像JSP页面大量嵌套Java代码,开发更简洁

整合Thymeleaf过程及步骤

首先,明确整合Thymeleaf需要用到模块依赖Jar包

其次,要初步了解SpringBoot解析渲染Thymeleaf的过程

1.整合Thymeleaf需要用到模块依赖Jar包:Web模块和Thymeleaf模块相应的依赖Jar包

<dependencies>
        <!--thymeleaf模块依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--Web模块依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
</dependencies>

上面的依赖在SpringBoot Initializer中勾选Web模块勾选Web,Themeleaf模块勾选Thymeleaf

2.构建整合Thymeleaf的目录结构

文件夹 说明
controller 在Application所在的包下建controller会被SpringBoot自动扫描配置Bean
static static是SpringBoot默认加载像CSS,JS,IMG等静态资源的文件夹
templates templates默认是放置模板文件,用来展示渲染的页面

3.controller的代码内容

@Controller告知SpringBoot这是控制层,自动注入Bean

@RequestMapping是映射访问路径注解,在localhost:8080之后的路径就是根据此处写的

package com.luojay.springbootthymeleaf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ThymeleafController {
    @RequestMapping("/getThymeleaf")
    public String Welcome(){
        return "luojay";
        //此处返回值,对应templates的文件名,SpringBoot根据它找到对应Html
    }
}

4.luojay.html代码内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>luojay's Thymeleaf</title>
</head>
<body>
    hello,luojay!
</body>
</html>

页面展示效果

进阶的学习案例

上面的入门例子可以说是简单到不行,有了这个基础,可以利用网上一些静态资源整合Thymeleaf搭建好看的页面,比如登录界面

文章配套代码

Github:springboot-thymeleaf

登录页面源代码:基于Layui简约登录界面

原文地址:https://www.cnblogs.com/codeluojay/p/12105125.html

时间: 2024-08-29 03:18:40

SpringBoot整合Thymeleaf(三)的相关文章

三、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

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

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

springboot整合Thymeleaf模板引擎

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

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学习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整合elasticsearch(三)

Docker安装elasticsearch 启动注意2点,1是内存,2是线程数(此处进行简单安装,后面会详细补充es文档) 1 [[email protected] ~]# docker images 2 REPOSITORY TAG IMAGE ID CREATED SIZE 3 elasticsearch latest 874179f19603 12 days ago 771 MB 4 springbootdemo4docker latest cd13bc7f56a0 2 weeks ago

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

swagger+springBoot整合集成(三步看明白)

swagger是什么? swagger是一个后台人员专门测试的一个简单工具,特别适合做前后端分离的项目,之前我们一直都是用的postman,但是这个有点复杂,路径需要自己,而swagger则直接使用,接下来我们就直接上代码说一说swagger,文采不好往见谅! 1.首先需要两个jar包 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifac