这里需要使用@Controller,不能使用@RestController,不然会以Json格式响应到页面,不会进行视图解析。
解决:template might not exist or might not be accessible by any of the configured Template Resolvers报错
在<!DOCTYPE html>下加上
<!--suppress ALL-->
把设置里面的 Editor->Inspections->Thymeleaf->Expression variables validation后面的勾去掉
红线消失!
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>{
}
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);
}
}
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 + "]";
}
}
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";
}
}
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>
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>
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>
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