谷粒商城-高级-41 商城业务-整合 thymeleaf 渲染首页
一、相关资源导入
1、引入thymeleaf依赖
gulimall-product/pom.xml
<!-- 模板引擎: thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、修改thymeleaf
在调试期间,需要将thymeleaf缓存关掉,方便查找问题。gulimall-product/src/main/resources/application.yml
spring:
datasource:
username: root
password: root
url: jdbc:mysql://192.168.10.10:3306/gulimall_pms
driver-class-name: com.mysql.cj.jdbc.Driver
# 配置nacos注册中心
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
jackson:
date-format: yyyy-MM-dd HH:mm:ss
thymeleaf:
cache: false # 调试期间,关闭缓存
3、导入静态资源
将我们前端需要的静态页面HTML导入 gulimall-product/src/main/resources/templates/
目录下,js和css文件导入到gulimall-product/src/main/resources/static/
目录下,在我们gulimall-product 目录下新建 web目录,用于前端调用,将原来的 /gulimall-product/controller/
作为API访问的Controller目录 改为 /gulimall-product/app/
。
4、热加载
引入热加载工具,页面修改不重启服务器实时更新
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
小结:
1)、thymeleaf-starter:关闭缓存
2)、静态资源都放在static文件夹下就可以按照路径直接访问了
3)、页面放在 templates 文件夹下,直接访问(springboot,访问项目时,默认会找index,webmvcAutoConfiguration.java 里边可以看到)
4)、页面修改不重启服务器实时更新
- 引入 dev-tools
- 修改完页面,controller + shift + f9 重新自动编辑页面
- 如果是代码配置改动了,推荐重启
前端首页:
为者常成,行者常至
自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)