Spring boot 集成thymeleaf

Spring boot 集成thymeleaf

背景

自己通过Spring boot集成通义千问实现了一个智能问答系统。Spring boot集成通义千问已经完成,现在需要做一个简单的页面展示,作为一个八年没有摸过前端的后端开发人员,不得不又拿起了html和thymeleaf。

Spring boot 集成web和thymeleaf

Spring boot使用thymeleaf开发前端页面,首先Spring boot得是个web项目,也就是需要集成web功能,然后就需要支持thymeleaf。‘

第一步添加依赖
       <!--spring boot 对web的支持-->
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Spring boot 对thymeleaf的支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--Spring boot对lombok的支持-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

Spring boot添加了这两个依赖之后,就具备了thymeleaf的web能力了。

第二步验证thymeleaf

添加完依赖,我需要写个controller实验一下,代码如下

@Controller
@RequestMapping("demo")
@Slf4j
public class DemoController {

    @RequestMapping("demo")
    public String demo(Model model){
        log.debug("demo ok");
        model.addAttribute("msg","hello thymeleaf");
        return "demo";
    }

}

controller写完之后,我需要写一个html实验一下,html位置在
在这里插入图片描述
html页面内容如下

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>测试Thymeleaf</title>
</head>
<body>
  <h1>测试 Thymeleaf模板 </h1>
  <h4>获取数据: <span th:text="${msg}"></span></h4>
</body>
</html>

展示效果

把项目启动后,访问controller的地址,就可以跳转到html页面中,并且带去了controller中的数据
在这里插入图片描述

总结

Spring boot集成thymeleaf是比较简单的,虽然现在都是vue和前后端分离的开发模式,不过,有些小项目和小demo用thymeleaf做还是挺不错的。

相关推荐

  1. Spring Boot集成thymeleaf快速入门demo

    2024-06-07 19:46:03       9 阅读
  2. Thymeleaf

    2024-06-07 19:46:03       20 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-07 19:46:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-07 19:46:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-07 19:46:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-07 19:46:03       20 阅读

热门阅读

  1. 从技术层面出发,如何确保云安全?

    2024-06-07 19:46:03       9 阅读
  2. Spark 之 HiveStrategies

    2024-06-07 19:46:03       9 阅读
  3. 设计模式之访问者模式

    2024-06-07 19:46:03       7 阅读
  4. Flask Web开发基础:数据库与ORM实战

    2024-06-07 19:46:03       10 阅读
  5. 视频拼接服务分享

    2024-06-07 19:46:03       9 阅读
  6. WPF学习笔记:给StackPanel加阴影

    2024-06-07 19:46:03       10 阅读
  7. 开发常用软件

    2024-06-07 19:46:03       9 阅读
  8. Python一般用什么IDE:深入剖析四大主流选择

    2024-06-07 19:46:03       9 阅读
  9. OpenCV 4.X 使用CvxText在图片显示汉字

    2024-06-07 19:46:03       8 阅读
  10. Less is more VS 精一 [生活感悟]

    2024-06-07 19:46:03       10 阅读