• SpringBoot中使用Thymeleaf


    这里需要使用@Controller,不能使用@RestController,不然会以Json格式响应到页面,不会进行视图解析。
    
    • 1

    解决:template might not exist or might not be accessible by any of the configured Template Resolvers报错

    IDEA中thymeleaf 表达式报红波浪线
    在这里插入图片描述

    <!DOCTYPE html>下加上
    <!--suppress ALL-->
    把设置里面的 Editor->Inspections->Thymeleaf->Expression variables validation后面的勾去掉
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    红线消失!
    在这里插入图片描述

    Model, ModelMap, and ModelAndView in Spring MVC

    UserMapper.java

    
    package com.leyou.mapper;
    
    import com.leyou.pojo.TbUser;
    import org.apache.ibatis.annotations.Mapper;
    
    @Mapper
    public interface UserMapper extends tk.mybatis.mapper.common.Mapper<TbUser>{
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    UserService.java

    
    package com.leyou.service;
    
    import com.leyou.mapper.UserMapper;
    import com.leyou.pojo.TbUser;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import java.util.List;
    
    @Service
    public class UserService {
    
        @Autowired
        private UserMapper userMapper;
    
        public List<TbUser> queryByObj(TbUser user){
            return this.userMapper.select(user);
        }
    
        public TbUser queryById(Long id){
            return this.userMapper.selectByPrimaryKey(id);
        }
    
        public List<TbUser> queryAll(){
            return this.userMapper.selectAll();
        }
    
        @Transactional
        public void deleteById(Long id){
            this.userMapper.deleteByPrimaryKey(id);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    TbUser.java

    package com.leyou.pojo;
    
    import javax.persistence.Id;
    import java.util.Date;
    
    public class TbUser {
        @Id
        private Long id;
    
        // 用户名
        private String userName;
    
        // 密码
        private String password;
    
        // 姓名
        private String name;
    
        // 年龄
        private Integer age;
    
        // 性别,1男性,2女性
        private Integer sex;
    
        // 出生日期
        private Date birthday;
    
        // 创建时间
        private Date created;
    
        // 更新时间
        private Date updated;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public Integer getSex() {
            return sex;
        }
    
        public void setSex(Integer sex) {
            this.sex = sex;
        }
    
        public Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    
        public Date getCreated() {
            return created;
        }
    
        public void setCreated(Date created) {
            this.created = created;
        }
    
        public Date getUpdated() {
            return updated;
        }
    
        public void setUpdated(Date updated) {
            this.updated = updated;
        }
    
        @Override
        public String toString() {
            return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", name=" + name
                    + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", created=" + created
                    + ", updated=" + updated + "]";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113

    ThymeleafController.java

    package com.leyou.controller;
    
    import com.leyou.pojo.TbUser;
    import com.leyou.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    @Controller
    public class ThymeleafController {
    
        @Autowired
        private UserService userService;
    
        @GetMapping("/all")
        public String all(ModelMap model) {
            // 查询用户
            List<TbUser> tbUsers = this.userService.queryAll();
            System.out.println("tbUsers = " + tbUsers);
            // 放入模型
            model.addAttribute("tbUsers", tbUsers);
            // 返回模板名称(就是classpath:/templates/目录下的html文件名)
            return "users.html";
        }
    
        @GetMapping("/showViewPage")
        public String passParametersWithModel(Model model) {
            Map<String, String> map = new HashMap<>();
            map.put("spring", "mvc");
            model.addAttribute("message1", "Baeldung");
            model.addAttribute("message2", "zhangsan");
            model.mergeAttributes(map);
            return "test.html";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    users.html

    DOCTYPE html>
    
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>首页title>
        <style type="text/css">
            table {border-collapse: collapse; font-size: 14px; width: 80%; margin: auto}
            table, th, td {border: 1px solid darkslategray;padding: 10px}
        style>
    head>
    <body>
    <div style="text-align: center">
        <span style="color: darkslategray; font-size: 30px">欢迎光临!span>
        <hr/>
        <table class="list">
    
            <tr>
                <th>idth>
                <th>姓名th>
                <th>用户名th>
                <th>年龄th>
                <th>性别th>
                <th>生日th>
               
            tr>
            <tr th:each="user : ${tbUsers}">
                <td th:text="${user.id}">1td>
                <td th:text="${user.name}">张三td>
                <td th:text="${user.userName}">zhangsantd>
                <td th:text="${user.age}">20td>
                <td th:text="${user.sex} == 1 ? '': ''">td>
                <td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd')}">1980-02-30td>
               
            tr>
    
        table>
    div>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    test.html

    DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    <table border="1">
        <tr>
            <th>表头1th>
            <th>表头2th>
        tr>
        <tr>
            <td th:text="${message1}" >默认值1td>
            <td th:text="${message2}" >默认值2td>
        tr>
    table>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    pom.xml

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
    
        <groupId>org.examplegroupId>
        <artifactId>springboot_demoartifactId>
        <version>1.0-SNAPSHOTversion>
    
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
          
            <version>2.1.7.RELEASEversion>
        parent>
    
        <properties>
            <java.version>1.8java.version>
        properties>
    
        <dependencies>
            
          <dependency>
                <groupId>org.springframework.securitygroupId>
                <artifactId>spring-security-webartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.securitygroupId>
                <artifactId>spring-security-configartifactId>
            dependency>
            <dependency>
                <groupId>commons-codecgroupId>
                <artifactId>commons-codecartifactId>
            dependency>
            <dependency>
                <groupId>org.apache.commonsgroupId>
                <artifactId>commons-lang3artifactId>
                <version>3.9version>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>1.18.8version>
                <scope>providedscope>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-loggingartifactId>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-jdbcartifactId>
            dependency>
    
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
            dependency>
            
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>1.3.2version>
            dependency>
            
          <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druid-spring-boot-starterartifactId>
                <version>1.1.10version>
            dependency>
            
           <dependency>
                <groupId>tk.mybatisgroupId>
                <artifactId>mapper-spring-boot-starterartifactId>
                <version>2.0.2version>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-thymeleafartifactId>
            dependency>
    
        dependencies>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92

    application.properties

    # ?置com.leyou包的日志???debug
    logging.level.com.leyou=debug
    
    # mybatis ?名?描
    mybatis.type-aliases-package=com.leyou.pojo
    
    # ?接四大参数
    spring.datasource.url=jdbc:mysql://localhost:3306/user_manager?serverTimezone=UTC
    spring.datasource.username=root
    spring.datasource.password=root
    # 可省略,SpringBoot自?推断
    spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
    
    spring.datasource.hikari.idle-timeout=60000
    spring.datasource.hikari.maximum-pool-size=30
    spring.datasource.hikari.minimum-idle=10
    
    #初始化?接数
    spring.datasource.druid.initial-size=1
    #最小空??接
    spring.datasource.druid.min-idle=1
    #最大活??接
    spring.datasource.druid.max-active=20
    #?取?接???是否可用
    spring.datasource.druid.test-on-borrow=true
    #?控?面??
    spring.datasource.druid.stat-view-servlet.allow=true
    
    # mapper.xml文件位置,如果没有映射文件,?注?掉
    #mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
    
    # ???段??thymeleaf的模板?存
    spring.thymeleaf.cache=false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    FusionSphere虚拟化解决方案介绍
    LeetCode220829_91、除自身以外数组的乘积
    基于PG-Oracle和MySQL的三库通用sql代码开发
    nacos -分布式事务-Seata** linux安装jdk ,mysql5.7启动nacos配置ideal 调用接口配合 (保姆级细节教程)
    java:SpringBoot入门
    爱上开源之golang入门至实战第三章goroutine分析
    AntDB数据并行加载工具的实现
    千变万化的Promise
    springnative让java应用脱离jvm
    【实战原创】Centos7下Samba服务器配置(实战)
  • 原文地址:https://blog.csdn.net/djydjy3333/article/details/126498388