SpringBoot整合Thymeleaf模板引擎【查询添加】(四)

Thymeleaf开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、OGNL表达式效果,避免每天套模板、改jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
Thymeleaf整合SpringBoot

这篇查询添加是基于上一篇spring boot整合mybatis做的哦,需要看完整的添加删除的小伙伴可以看下上一篇的哦
  1. 在pom.xml文件引入thymeleaf
<dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-thymeleaf</artifactId>
       </dependency>
  1. controller
@Controller
public class DeptController {
   @Autowired
   private DeptService deptService;

   /**
    * 查询
    * @param model
    * @return
    */
   @RequestMapping("/")
   public String findAll(Model model){
       List<Dept> allDept = deptService.findAllDept();
       model.addAttribute("dept",allDept);
       return "index";
   }

   /**
    * 删除
    * @param deptno
    * @return
    */
   @RequestMapping("/deleteDept")
   public String deleteDept(Integer deptno){
       int i = deptService.deleteById(deptno);
       return "redirect:/";
   }

   /**
    * 添加
    * @param dept
    * @return
    */
   @RequestMapping("/addDept")
   public String addDept(Dept dept){
       int i = deptService.addDept(dept);
       return "redirect:/";
   }
}
  1. 新建编辑模板文件,在resources文件夹下的templates目录,用于存放HTML等模板文件,在这新增indexhtml,添加如下代码。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
 <table border="1" cellspacing="0">
     <tr>
         <td>编号</td>
         <td>名称</td>
         <td>地址</td>
         <td>操作</td>
     </tr>
     <tr th:each="dept:${dept}">
         <td th:text="${dept.deptno}"></td>
         <td th:text="${dept.dname}"></td>
         <td th:text="${dept.loc}"></td>
         <td><a th:href="@{/deleteDept(deptno=${dept.deptno})}" th:text="删除"></td>
     </tr>
 </table>
 <hr>
<form th:action="@{/addDept}" th:method="post">
    名称:<input type="text" th:name="dname"/></br>
    地址:<input type="text" th:name="loc"/></br>
    <input type="submit" value="添加">
</form>
</body>
</html>

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值