SpringBoot--thymeleaf

1、导包

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

2、Controller中对视图的处理

复制代码
@ResponseBody
    @GetMapping(value ="/thymeleaf")
    public ModelAndView test2(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("menglongdeyeIndex");
        mv.addObject("title","朦胧的夜的第一个web页面");
        mv.addObject("desc", "欢迎进入朦胧的夜的系统");
        User user = new User();
        user.setUsername("朦胧的夜");
        user.setPassword("123");
        mv.addObject("user", user);
        return mv;
    }
复制代码

3、页面获取视图的值

  文件要创建在resource/templates目录下

复制代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <!-- 可以看到 thymeleaf 是通过在标签里添加额外属性来绑定动态数据的 -->
    <title th:text="${title}">Title</title>
    <!-- 在/resources/static/js目录下创建一个hello.js 用如下语法依赖即可-->
    <script type="text/javascript" th:src="@{/js/hello.js}"></script>
</head>
<body>
<h1 th:text="${desc}">Hello World</h1>
<h2>=====作者信息=====</h2>
<p th:text="${user?.username}"></p>
<p th:text="${user?.password}"></p>
</body>
</html>
复制代码

 

posted @   李聪龙  阅读(142)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示