梦入琼楼寒有月,行过石树冻无烟

Spring boot Web and Thymeleaf 开发环境构建

在本文之中,我们主要使用spring tool suite来构建spring boot 的web开发环境,通过Eclipse集成开发环境中选择New->Other->Spring Boot->Spring Starter Project 下来构建Spring web项目,通过Spring tool suite远程访问https://start.sring.io/来构建spring boot应用(```这个过程虽然是gui形式但是依然需要联网,否则无法从start.spring.io中获取spring boot包),通过Spring tool suite图形化构建spring boot开发环境。

在spring boot 版本之中,需要对创建完后的spring boot项目中的pom.xml文件进行编辑添加其web支持,而spring boot非常的遵守开箱即用少配置的原则自动帮我们对在项目根木目录下的文件pom.xml添加了spring boot web支持:

1
2
3
4
5
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Thymeleaf 创建

Thymeleaf 和 FreeMarker一样,都是一款模板引擎,而与之不同的是Thymeleaf是适用于独立环境和现代服务器的模板引擎,并不是说FreeMarker不好,而是说Thymeleaf是专门适用与Java的,并对Spring MVC提供了完美的支持。除此之外,Spring boot的Web应用还支持包括 FreeMarker,Thymeleaf,Groovy,Velocity、Mustache等模板引擎。

在本文之中我们主要介绍Thymeleaf的特点及概括,Thymeleaf的主要目标和工作居士为当前开发的项目中的代码格式带来优雅,自然的HTML,并可在浏览器中正常现实,并作为静态原型工作,从而可以加强团队之间的协作。Thymeleaf 的运行方式有两种,一种为静态一种为动态,读者在学习Thymeleaf之中可以深刻的理会到这个原理,虽然心情波折但是依然相差无几。

Spring boot Thymeleaf 创建

在spring boot 5 中,WebFlux的出现对Web应用不再唯一,所以spring-boot-starter-thymeleaf依赖将不会出包含在spring-boot-starter-web模块中,所以需要开发者手动在开集成开发环境中进行添加和构建,说的通俗易懂点就是在New Spring Starter Project -> New Spring Starter Project Dependencies [Spring Web and Thymeleaf]即可,在New Spring Starter Project Dependencies中选择或搜索Spring Web 、Thymeleaf并勾选然后创建即可。

Themeleaf 目录

在创建spring boot web开发环境后,通常会生成关于Thymeleaf文件的目录在src/main/resources下如:static、templates其中static通常用于存放js、css、img等,而在templates目录下主要用于存放视图页面。

输出

通过在 src/main/java/中创建软件包为com.example.controller的控制器类:

src/main/java/

TestThymeleafController.java
1
2
3
4
5
6
7
8
9
10
11
12
package com.example.controller;

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

@Controller
public class TestThymeleafController {
@RequestMapping("/")
public String test() {
return "index";
}
}

src/main/resource/templates

index.html
1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello,Spring boot</title>
</head>
<body>
Test Spring boot The Thymeleaf
</body>
</html>

Thymeleaf Eclipse 引入

在默认的情况下,Eclipse是不支持Thymeleaf语法的,所以我们需要手动添加eclipse即集成开发环境对Thymeleaf语法的支持和提供代码提示等功能,通常在没有代码提示的时候开发Thymeleaf是一件非常无语的事情,比写spring mvc还无语,安装Thymmeleaf插件的方法有很多,如通过远程和离线进行安装,在eclipse开发环境中选择 Help->Install New Software……,中可添加url和本地进行安装

远程下载

在 install New Software……中的url处填写 http://www.thymeleaf.org/eclipse-plugin-update-site/,然后在name处输入Thymeleaf之后通过一系列下一步完成同意协议操作即可。

离线下载

离线下载即在你有网的时候下,然后安装时是不需要网络的,其方法和远程下载无几,https://github.com/thymeleaf/thymeleaf-extras-eclipse-plugin 在此处进行下载,然后将下载的zip包按照远程下载的方式在 Install New Software…… -> Add ->url 处替换为你下载的zip文件即可,然后步骤和远程下载相差无几。

Spring boot Thymeleaf 刷新

在开发Thymeleaf之中会遇到一个非常无语的问题,就是说当更改完Thymeleaf时,需要手动重启Spring boot服务,而Spring boot服务又需要调用Tomcat服务应用,会造成一定的消耗,所以我们本次以IDEA环境为例(不要问我为什么之前用Eclipse,现在转Idea,满满的你就会知道了

application.properties

application.properties 文件一般存储在 src/main/resources/目录下,无论在eclipse或idea环境中他都是一片叶子,我们在其配置:

1
spring.thymeleaf.cache=false

即关闭spring boot right thymeleaf的缓存,这样更新Thymeleaf文件后在其本地环境就会变得不一样了

Idea Build

如果你是Idea 环境那就是另一种福音了,我们可以通过Idea集成开发环境进行一定的配置如:
File->Settings->Build,Execution,Deployment->Compiler
标签内将Build project automatically勾选,即“自动构建项目”.,然后点击应用(Apply)即可

Maintenance and Registry

使用快捷键 Shift+Ctrl+Alt+/即可唤出Maintenance页面,然好点击Registry……标签将key为compiler.automake.allow.when.app,running在Value中打个勾之后关闭即可,然后重启Idea。

⬅️ Go back