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>
原文地址:https://www.cnblogs.com/liconglong/p/11715467.html