版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21891465/article/details/82188277
一.概述:
Thymeleaf是一种模板语言,可以动态或者静态显示文本内容,本例主要讲解如何在SpringBoot项目中引入Thymeleaf。
二.环境:
SpringBoot +maven+mysql+Thymeleaf
三.步骤:
- 在项目pom.xml文件中引入Thymeleaf依赖
- <!-- thymeleaf模板引擎 S -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- thymeleaf模板引擎 E -->
- <!-- thymeleaf模板引擎 S -->
- 在application.yml配置文件中添加Thymeleaf配置
- # thymeleaf模板配置 S
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML5
encoding: UTF-8
cache: false #热部署文件,页面不产生缓存,及时更新
resources:
chain:
strategy:
content:
enabled: true
paths: /**
# thymeleaf模板配置 E
- # thymeleaf模板配置 S
- 使用Thymeleaf展示数据
- User实体类
package com.kiboy.bean; import java.util.Date; public class User { private String id; private String code; private String name; private String password; private int status; private String loginid; private Date gmt_create; private Date gmt_modified; public String getId() { return id; } public void setId(String id) { this.id = id == null ? null : id.trim(); } public String getLoginid() { return loginid; } public void setLoginid(String loginid) { this.loginid = loginid; } public Date getGmt_create() { return gmt_create; } public void setGmt_create(Date gmt_create) { this.gmt_create = gmt_create; } public Date getGmt_modified() { return gmt_modified; } public void setGmt_modified(Date gmt_modified) { this.gmt_modified = gmt_modified; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } }
- UserService接口
package com.kiboy.service; import java.util.List; import org.springframework.stereotype.Service; import com.kiboy.bean.User; @Service public interface UserService { public List<User> getUser(); public List<User> getAllUser(); public int addUser(User user); public List<User> getUserById(String id); public int delUser(String id); }
-
UserMapper
package com.kiboy.mapper; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import com.kiboy.bean.User; @Mapper public interface UserMapperNoXml { @Delete("delete from user where id=#{id}") int deleteByPrimaryKey(String id); @Insert("INSERT INTO USER (code,name,loginid,password,status)VALUES(#{code},#{name},#{loginid},#{password},#{status})") int insert(User u); @Select("SELECT * FROM USER WHERE ID=#{id}") User selectByPrimaryKey(String id); int updateByPrimaryKeySelective(User u); @Update("UPDATE USER SET code=#{code},name=#{name},status=#{status} where id=#{id}") int updateByPrimaryKey(User u); @Select("SELECT * FROM USER") List<User> selectAll(); }
-
UserService实现类
package com.kiboy.service.impl; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.kiboy.bean.User; import com.kiboy.mapper.UserMapperNoXml; import com.kiboy.service.UserService; @Service public class UserServiceImplNoXml implements UserService{ @Autowired private UserMapperNoXml mapper; public List<User> getUser(){ List<User> list = new ArrayList<User>(); list.add(mapper.selectByPrimaryKey("1")); return list; } public List<User> getAllUser(){ List<User> list = new ArrayList<User>(); list = mapper.selectAll(); return list; } public int addUser(User user) { return mapper.insert(user); } public int delUser(String id) { return mapper.deleteByPrimaryKey(id); } public List<User> getUserById(String id) { return null; } }
-
UserController
package com.kiboy.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import com.kiboy.bean.User; import com.kiboy.service.impl.UserServiceImplNoXml; @RestController @RequestMapping("/UserNoXml") public class UserControllerNoXml { @Autowired private UserServiceImplNoXml userService; /** * 获取用户列表 * @return */ @GetMapping("/userList") public ModelAndView getAllUser(Model model) { List<User> list = userService.getAllUser(); int num = list.size(); if(null!=list && num>3){ for (int i = 0; i < num-3; i++) { list.remove(0); } } model.addAttribute("user", list.get(0)); model.addAttribute("userList", list); model.addAttribute("title", "用户信息列表"); return new ModelAndView("user/userList", "userModel", model); }
-
前台页面(具体的Thymeleaf语法可以自行百度下,在后续的文章中会做解释,这里先照着用就好)
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" > <head> <meta charset="UTF-8"> <title>welcome</title> </head> <body> <h3 th:text="${userModel.title}">Welcome to springboot</h3> <table border="12"> <thead> <tr> <td>ID</td> <td>Code</td> <td>Name</td> </tr> </thead> <tbody> <tr th:if="${userModel.userList.size()} eq 0"> <td colspan="3">没有用户信息!!</td> </tr> <tr th:each="user : ${userModel.userList}"> <td th:text="${user.id}">-1</td> <td th:text="${user.code}">空</td> <td th:text="${user.name}">空</a> </td> </tr> </tbody> </table> </body> </html>
-
启动应用访问页面http://127.0.0.1:20001/UserNoXml/userList,显示如下
- User实体类