本节将和大家一起实战Spring Boot 2.0 和thymeleaf 模板引擎

1. 创建项目

2. 使用Spring Initlizr 快速创建Spring Boot 应用程序

3. 填写项目配置信息

4. 添加Web 模块

5. 添加thymeleaf 模块

6. 项目保存路径

7. POM.xml 添加thymeleaf依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.xingyun</groupId>
<artifactId>srping-boot-with-thymeleaf-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>srping-boot-with-thymeleaf-sample</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <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> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

8. 配置application.properties

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
# 关闭缓存,即时刷新,上线生产环境需改成true
spring.thymeleaf.cache=false

9. 创建模板文件

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body> this is index.html
</body>
</html>

welcome.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
this is welcome.html
</body>
</html>

10.创建Controller

package com.xingyun.srpingbootwiththymeleafsample;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class HomeController { @GetMapping(value = "/")
public String index(){
return "/views/index";
} @GetMapping(value = "/welcome")
public String welcome(){
return "/views/welcome";
}
}

11. 访问 http://127.0.0.1:8080

12. 访问http://127.0.0.1:8080/welcome

本篇博文源码

Spring Boot 2.0 整合Thymeleaf 模板引擎的更多相关文章

  1. Spring Boot 2.0 整合 FreeMarker 模板引擎

    本篇博文将和大家一起使用Spring Boot 2.0 和FreeMarker 模板引擎整合实战. 1. 创建新的项目 2. 填写项目配置信息 3. 勾选web 模块 4. 勾选freemarker模 ...

  2. Spring Boot Web开发与thymeleaf模板引擎

    简介: 使用Springboot应用,选中需要的模块, Spring已经默认将场景配置好了,只需在配置文件中少量配置就可以运行起来 自己编写业务代码 自动配置原理 这个场景Springboot帮我们配 ...

  3. Spring Boot Web开发中Thymeleaf模板引擎的使用

    这里使用的是idea 1.新建Spring Boot项目 File-->New-->Project...,然后选择左边的Spring Initializr-->Next,可根据自己的 ...

  4. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  5. spring boot 2.0 整合 elasticsearch6.5.3,spring boot 2.0 整合 elasticsearch NoNodeAvailableException

    原文地址:spring boot 2.0 整合 elasticsearch NoNodeAvailableException 原文说的有点问题,下面贴出我的配置: 原码云项目地址:https://gi ...

  6. Spring Boot 2.0 整合携程Apollo配置中心

    原文:https://www.jianshu.com/p/23d695af7e80 Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够 ...

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

    在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: ...

  8. Spring Boot WebFlux-04——WebFlux 整合 Thymeleaf

    第04课:WebFlux 整合 Thymeleaf 上一篇介绍的是用 MongoDB 来实现 WebFlux 对数据源的操作,那么有了数据需要渲染到前台给用户展示,这就是本文关心的 View 层,Vi ...

  9. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

随机推荐

  1. 菜单联动,select联动菜单(搜索之后默认选中)

    框架:thinkphp php控制器代码: $schedulelist = M('schedule')->getField('sid,schedule_name'); $this->ass ...

  2. centos关机、重启、图形界面与命令行界面切换命令

    1.关机: init0;  poweroff;  halt;  shutdown 2.重启: init1;  reboot; 3.图形界面切换到命令行界面: init3; 或者,修改配置文件: #vi ...

  3. web 连接池配置

    TOMCAT J2EE项目连接池配置 web 项目的 web.xml <web-app> <resource-ref> <description>DB Connec ...

  4. VB.NET只允许打开一个实例

    If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) ...

  5. Python3面向对象——案例-01

    经典的策略模式案例 问题描述 使用"策略"设计模式处理订单折扣的 UML 类图 定义一系列算法,把它们一一封装起来,并且使它们可以相互替换.本模式使得算法可以独立于使用它的客户而变 ...

  6. java后端发送请求

    package com.ty.mapapisystem.util; import java.io.BufferedReader;import java.io.FileInputStream;impor ...

  7. 2017-2018-20172309 『Java程序设计』课程 结对编程练习_四则运算——第一周

    2017-2018-20172309 『Java程序设计』课程 结对编程练习_四则运算 组队成员: 仇夏 学号: 20172310 博客地址:点击这里 1. 需求分析: 可生成题目: - 输入要想生成 ...

  8. 淘宝bug bug bug

    手机淘宝(苹果版)出现了一个挺耀眼的bug...... 待评价有九个,如图 点进去评价6个之后,还有三个 再出来,待评价还是有九个. 重新打开,下拉刷新都不能改变

  9. CocosCreator核心概念里的基础点

    1,锚点:是节点位置的参照点,也是自身旋转,缩放的基准点,也是子节点坐标原点.X.Y描述横纵向的锚点位置.注:锚点的取值是可以超过(0,0)~(1,1),即锚点并不在节点尺寸范围内. 2,子节点:子节 ...

  10. C#高级编程9 第16章 错误和异常

    C#高级编程9 第16章 错误和异常 了解这章可以学会如何处理系统异常以及错误信息. System.Exception类是.NET运行库抛出的异常,可以继承它定义自己的异常类. try块代码包含的代码 ...