spring boot 中添加模版
  4QHOBLSsnlZu 2023年11月05日 127 0

Yeah! 本文,我们谈谈如何在 spring boot 中添加模版,因为有时候我们也是需要后端渲染的嘛,比如公司官网,好吧~我听到某位前端小伙伴说用 node 呀,也行~

But,今天我们讲的是 spring boot

添加依赖

案例在之前项目 Spring Boot 整合 Swagger 接口文档工具 基础上进行整改~

这里我们添加模版依赖:

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

添加模版

我们添加测试的模版。

resources/templates 文件夹下新建 admin/index.html 文件:

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

这里我们简单设置模板,message 假设是数据库返回的数据~

添加映射

我们紧接着编写 Controller

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

@Controller
@RequestMapping("admin")
public class SysUserController {
    @RequestMapping("/index")
    public String Index(Model model) {
        model.addAttribute("message", "Hello, Spring Boot Thymeleaf!");
        return "admin/index";
    }
}

我们指定了路由的访问路径为 admin/index,然后其对应的模版文件是 resources 下面的 admin/index.html 文件,上面代码映射的模版路径 return "admin/index"

效果

完整操作后,启动项目,可得到下面的结果。

spring boot 中添加模版_前端

当然,页面很丑,但是不影响我们对该知识点的吸收。

感谢阅读~

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  2Vtxr3XfwhHq   2024年05月17日   53   0   0 Java
  Tnh5bgG19sRf   2024年05月20日   109   0   0 Java
  8s1LUHPryisj   2024年05月17日   46   0   0 Java
  aRSRdgycpgWt   2024年05月17日   47   0   0 Java
4QHOBLSsnlZu