• 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>
  • 相关阅读:
    文件操作:根据现有类生成所需要的类
    Microsoft JScript 运行时错误: “”未定义
    未完成
    WPF模拟雷达界面效果图
    WebClient模拟网页提交表单
    201319
    Delphi中的InStrRev函数(倒找文本)
    利用IDhttp实现图片下载
    白话解释哈希表
    整理的Delphi常用字符串函数
  • 原文地址:https://www.cnblogs.com/liconglong/p/11715467.html
Copyright © 2020-2023  润新知