springboot 整合 thymeleaf 页面
程序员文章站
2022-05-01 22:05:24
...
1、添加maven依赖
<!-- 用于解析html -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、配置文件:application.properties
##thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.mvc.view.suffix=.html
spring.mvc.static-path-pattern=/static/**
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML
spring.devtools.restart.enabled=true
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.enabled=true
3、目录结构
在resources下 新建 static & templates 两个目录,
static保存静态资源文件,templates保存html文件
4、新建Controller 访问路径
@Controller
public class IndexController {
@RequestMapping({"/","/home"})
public String index(HashMap map){
return "home";
}
}
访问localhost:8001/home 会跳转到home.html页面,
@ResponseBody
方法注解有这个,说明以json传值,不会返回页面,只返回json字符串。一般ajax会用到。
类注解 @Controller + 方法注解 @ResponseBody
相当于 类注解@RestController
5、新建页面 home.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">//代表 使用thymeleaf框架
<head>
<meta charset="utf-8">
</head>
<body class="sticky-header">
Hello,Springboot!
</body>
</html>
静态资源引入路径:/static/js/...
推荐阅读
-
springboot整合shiro多验证登录功能的实现(账号密码登录和使用手机验证码登录)
-
SpringBoot整合Druid、Redis的示例详解
-
SpringBoot无废话入门04:MyBatis整合
-
MyEclipse下SpringBoot+JSP整合过程及踩坑
-
SpringBoot轻松整合MongoDB的全过程记录
-
springboot、mybatisplus框架整合搭建
-
springboot整合MybatisPlus基本使用
-
SpringBoot整合MyBatis获得插入数据后获取主键,返回值总是1
-
从.Net到Java学习第六篇——SpringBoot+mongodb&Thymeleaf&模型验证
-
SpringBoot项目整合mybatis的方法步骤与实例