springboot+thymeleaf模板
一、介绍
1.1 springboot
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
1.2 thymeleaf
Thymeleaf是一个Java XML / XHTML / HTML5模板引擎,可以在Web(基于servlet)和非Web环境中工作。它更适合在基于MVC的Web应用程式的视图层提供XHTML / HTML5,但它甚至可以在离线环境中处理任何XML档案。它提供完整的Spring Framework。
在Web应用程式中,Thymeleaf旨在成为JavaServer Pages(JSP)的完全替代品,并实现自然模板的概念:模板档案可以直接在浏览器中打开,并且仍然可以正确显示为网页。
二、实现
2.1文件目录
2.2 配置Maven
pom.xml的文件配置
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.graduation</groupId>
<artifactId>practice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>practice</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.3 thymeleaf配置
我们资源文件下面配置application.yml用于资源的引入
application.yml
spring:
thymeleaf:
mode: HTML
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
application.properties文件配置
# 静态资源
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/templates/
2.4 监听和配置文件
Userhandler.java监听文件
package com.graduation.practice.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@Controller
public class UserHandler {
@GetMapping("/index1")
public String index(Model model) {
return "/pages/home";
}
}
Practiceapplication.java的启动文件
package com.graduation.practice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PracticeApplication {
public static void main(String[] args) {
SpringApplication.run(PracticeApplication.class, args);
}
}
2.5 thymeleaf模板文件
index.html文件
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!--xmlns:th="http://www.w3.org/1999/xhtml"-->
<head>
<meta charset="UTF-8">
<title>主页</title>
</head>
<body>
<h1>
Hello World! 哥哥!
</h1>
<table>
<tr>
<th>用户ID</th>
<th>用户姓名</th>
<th>用户年龄</th>
</tr>
<!-- <tr th:each="user : ${list}">-->
<!-- <td th:text="${user.id}">2</td>-->
<!-- <!– <td th:text="'用户名是'+${user.username}+'===='">李四</td>–>-->
<!-- <td th:text="|用户名是, ${user.username}|">李四</td>-->
<!-- <td th:text="${user.age}">20</td>-->
<!-- </tr>-->
</table>
</body>
</html>
三、总结
3.1 @Controller和@RestController
在配置的过程中,遇到一个渲染的时候只能出现文字,而不可以映射到html文件。这里说一下两者的不同
3.1.1 @Controller
@Controller是模板的映射,可以映射到我们的templates下,return的结果会在templates下面的.html文件查找。
3.1.2 @RestController
@RestController是@Controller+@ResponseBody的结合体,返回的结果是一个json数据或者是string类型的数据。
因此在进行模板渲染的时候需要用到Controller,在数据传递的时候需要用到RestController。
3.2 启动文件
我们的启动文件一定要在项目文件下的一个文件夹内,不能直接放在项目文件下,否则项目就会出现扫描失败,从而不能启动。