SpringBoot工程配置Thymeleaf前端模板

这篇博客详细介绍了如何在SpringBoot项目中配置Thymeleaf作为前端模板引擎,包括在pom.xml中添加Thymeleaf依赖,更新application.yml配置,修改HelloController以及创建并设置index.html页面。通过这些步骤,最终能够在本地启动工程并在浏览器中预览效果。

本文为SpringBoot工程配置Thymeleaf前端模板

在pom.xml文件中添加Thymeleaf依赖

	//集成Thymeleaf
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>


修改application.yml文件

server:
  port: 9000

# thymeleaf
spring:
  thymeleaf:
    encoding: UTF-8
    prefix: classpath:/templates/
    check-template-location: true
    suffix: .html
    servlet:
      content-type: text/html
    mode: HTML5
    cache: false

修改HelloController.java文件中的hello方法

@RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(Model model){

        model.addAttribute("content","hello world");
        return "index";
    }

在templates文件夹下创建index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p th:text="${content}"></p>
</body>
</html>

启动工程,在浏览器输入http://localhost:9000/hello,结果如下
在这里插入图片描述完成Thymeleaf添加

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值