美文网首页右耳菌-邓小白的Java架构师的修炼之路
Thymeleaf在Springboot中的集成(一个简单的例子

Thymeleaf在Springboot中的集成(一个简单的例子

作者: 右耳菌 | 来源:发表于2022-05-05 21:27 被阅读0次
1. 创建SpringBoot项目
  • pom.xml 文件新增依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
  • 修改properties
#关闭thymeleaf的缓存,避免开发过程中修改页面不生效的情况,在部署的时候可以取消
spring.thymeleaf.cache=false
2. 创建welcome.html (src/main/resources/templates)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome</title>
</head>
<body>
    <h3>Hello, [[${username}]]</h3>
</body>
</html>
3. 创建WelcomeController
package com.study.mybatis.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/5/5 21:18
 */
@Controller
public class WelcomeController {

    @RequestMapping("/welcome")
    public String welcome(ModelMap modelMap) {
        modelMap.addAttribute("username", "Neco Deng");
        return "welcome";
    }
}
4. 测试(运行并且访问 http://localhost:8080/welcome)
  • 测试

如果觉得有收获就点个赞吧,更多知识,请点击关注查看我的主页信息哦~

相关文章

网友评论

    本文标题:Thymeleaf在Springboot中的集成(一个简单的例子

    本文链接:https://www.haomeiwen.com/subject/jimoyrtx.html