1.application.yml 配置
spring:
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: LEGACYHTML5
cache: false
server:
port: 29000
2.pom.xml配置
<!--支持跳转,springboot推荐使用thymeleaf模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3.controller写法
package com.xjn.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class PageController {
//通过controller返回html界面
@RequestMapping("/test")
public String testJumpPage(){
return "test";
}
//通过controller返回html界面
@RequestMapping("/index")
public String indexJumpPage(){
return "index";
}
}
4.html写法,html位置:demosrcmain esources emplatesindex.html
demosrcmain esources emplates est.html
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>spring boot web project</title>
</head>
<body>
<div>spring boot web project index.html</div>
<!--图片位置在 demosrcmain
esourcesstaticimga123 -->
<img src="img/a123.jpg">
</body>
</html>
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>spring boot web project</title>
</head>
<body>
<div>==================spring boot web project test=======================</div>
</body>
</html>
5.启动项目,浏览器访问html,通过controller返回html界面:
http://localhost:29000/index
http://localhost:29000/test
本文展示了如何在SpringBoot项目中配置Thymeleaf,包括application.yml设置、pom.xml依赖引入,以及Controller中返回HTML页面的方法。同时,提供了index.html和test.html两个页面的示例,启动项目后,可以通过http://localhost:29000/index和http://localhost:29000/test访问。
2369

被折叠的 条评论
为什么被折叠?



