SpringBoot使用Thymeleaf模板引擎访问静态html的过程

2025-11-08 0 118

最近要做一个java web项目,因为页面不是很多,所以就没有前后端分离,前后端写在一起,这时候就用到thymeleaf了,以下是不动脑式的傻瓜教程。。。。。

一:创建spring boot的web项目,过程略;

二:依赖如下:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>springbootstarterweb</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>springbootstarterthymeleaf</artifactId> <version>2.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>springbootstartertest</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junitvintageengine</artifactId> </exclusion> </exclusions> </dependency>

三:配置文件:application.properties

#端口号 server.port=8099 # 配置 #thymeleaf spring.thymeleaf.cache=false spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.checktemplatelocation=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF8 spring.thymeleaf.servlet.contenttype=text/html spring.thymeleaf.mode=HTML

四:项目的templates文件夹下新建页面success.html,如下

SpringBoot使用Thymeleaf模板引擎访问静态html的过程

五:controller

import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; /** * @author liuhongyang * @2020/10/20 14:35 * 文件说明: */ @Controller public class FirstTestController { @RequestMapping(value = “hello”) public String hello(ModelMap modelMap) { modelMap.put(“hei”, “thymeleaf”); return “success”; } }

六:访问如下,完成

SpringBoot使用Thymeleaf模板引擎访问静态html的过程

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

遇见资源网 后端开发 SpringBoot使用Thymeleaf模板引擎访问静态html的过程 https://www.ox520.com/5332.html

常见问题

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务