FreeMarker常用指令(遍历List集合)
这里使用.ftlh渲染出来的界面,属性如果为空值,则显示不出来。
需要在ftlh中引入这个指令。
<#setting classic_compatible=true>
Controller:
//查询全部员工信息,展示 @RequestMapping("/showEmp") public ModelAndView testList(){ ModelAndView mv=new ModelAndView(); List<Emp> list=empService.findAll(); System.out.println(list.toString()); mv.addObject("empList",list); mv.setViewName("showEmp"); return mv; }
showEmp.ftlh:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> #empTable { width: 80%; border: 1px solid blue; margin: 0px auto; } #empTable th, td { border: 1px solid green; text-align: center; } </style> </head> <body> <table id="empTable" cellpadding="0px" cellspacing="0px"> <tr> <th>索引</th> <th>工号</th> <th>姓名</th> <th>岗位</th> <th>薪资</th> <th>部门号</th> </tr> <#list empList as emp> <#setting classic_compatible=true> <tr> <#-- //通过_index可以得到循环的下标--> <td>${emp_index}</td> <td></td> <td>${emp.ename}</td> <td>${emp.job}</td> <td>${emp.sal}</td> <td>${emp.deptno}</td> </tr> </#list> </table> </body> </html>
FreeMarker遍历Map集合
Controller:
//查询全部员工信息,展示 @RequestMapping("/showEmpMap") public ModelAndView testMap(){ ModelAndView mv=new ModelAndView(); List<Emp> list=empService.findAll(); Map<String,Emp> empMap=new HashMap<>(); for (Emp emp : list) { empMap.put(emp.getEmpno().toString(),emp); } mv.addObject("empMap",empMap); mv.setViewName("showEmpMap"); return mv; }
showEmpMap.ftlh:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> #empTable { width: 80%; border: 1px solid blue; margin: 0px auto; } #empTable th, td { border: 1px solid green; text-align: center; } </style> </head> <body> <#setting classic_compatible=true> <#--查看Map集合中单独一个元素--> <table id="empTable" cellpadding="0px" cellspacing="0px"> <tr> <th>索引</th> <th>工号</th> <th>姓名</th> <th>岗位</th> <th>薪资</th> <th>部门号</th> </tr> <#list empMap?keys as k> <tr> <td>${k_index}</td> <td>${k}</td> <td>${empMap[k].ename}</td> <td>${empMap[k].job}</td> <td>${empMap[k].sal}</td> <td>${empMap[k].deptno}</td> </tr> </#list> </table> </body> </html>
FreeMarker在遍历map集合是,key必须是String
FreeMaker内置函数:
内建函数语法格式: 变量+?+函数名称
显示年月日: ${today?date} 显示时分秒: ${today?time} 显示日期+时间:${today?datetime} 自定义格式化: ${today?string("yyyy年MM月")}
SpringBoot整合Thymeleaf
导入依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>2.4.5</version> </dependency>
showEmp.html:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" > <head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> #empTable { width: 80%; border: 1px solid blue; margin: 0px auto; } #empTable th, td { border: 1px solid green; text-align: center; } </style> </head> <body> 展示单个员工信息 工号:<span th:text="${emp.empno}"></span><br> 姓名:<span th:text="${emp.ename}"></span><br> 职务:<span th:text="${emp.job}"></span><br> 上级:<span th:text="${emp.mgr}"></span><br> 入职日期:<span th:text="${emp.hiredate}"></span><br> <hr/> <table id="empTable" cellpadding="0px" cellspacing="0px" > <tr> <th>工号</th> <th>姓名</th> <th>职务</th> <th>上级</th> <th>入职日期</th> <th>工资</th> <th>补助</th> <th>部门</th> <th>操作</th> </tr> <tr th:each="emp:${empList}"> <td th:text="${emp.empno}"></td> <td th:text="${emp.ename}"></td> <td th:text="${emp.job}"></td> <td th:text="${emp.mgr}"></td> <td th:text="${emp.hiredate}"></td> <td th:text="${emp.sal}"></td> <td th:text="${emp.comm}"></td> <td th:text="${emp.deptno}"></td> <td> <a th:href="@{removeEmp(empno=${emp.empno},ename=${emp.ename})}" >删除</a> <a href="javascript:void(0)" th:onclick="removeEmp([[${emp.empno}]],[[${emp.ename}]])">删除</a> <a href="javascript:void(0)" th:onclick="removeEmp([[${emp.empno}]],[[${emp.ename}]])">删除</a> </td> </tr> </table> <script> function removeEmp(empno,ename){ var resulet= confirm("确定要删除编号为“+empno+”的"+ename); if(resulet){ window.location.href="removeEmp?empno="+empno+"&ename="+ename; } } </script> </body> </html>
Thymeleaf内置对象
Thymeleaf提供了一些内置对象,内置对象可直接在模板中使用。这些对象是以#引用的。
使用内置对象的语法
1引用内置对象需要使用#
2大部分内置对象的名称都以s结尾。如:strings、numbers、dates
3常见内置对象如下
#arrays:数组操作的工具;
#aggregates:操作数组或集合的工具;
#bools:判断boolean类型的工具;
#calendars:类似于#dates,但是是java.util.Calendar类的方法;
#ctx:上下文对象,可以从中获取所有的thymeleaf内置对象;
#dates:日期格式化内置对象,具体方法可以参照java.util.Date;
#numbers: 数字格式化;#strings:字符串格式化,具体方法可以参照String,如startsWith、contains等;
#objects:参照java.lang.Object;
#lists:列表操作的工具,参照java.util.List;
#sets:Set操作工具,参照java.util.Set;#maps:Map操作工具,参照java.util.Map;
#messages:操作消息的工具。
<span th:text="${#httpServletRequest.getAttribute('msg')}"></span><br/> <span th:text="${#request.getAttribute('msg')}"></span><br/> <span th:text="${msg}"></span><br/> session:<br/> <span th:text="${#httpSession.getAttribute('msg')}"></span><br/> <span th:text="${#session.getAttribute('msg')}"></span><br/> <span th:text="${session.msg}"></span><br/> application:<br/> <span th:text="${#servletContext.getAttribute('msg')}"></span><br/> <span th:text="${application.msg}"></span><br/>
SpringBoot热部署
热部署之后则不需要重启项目,当监听到内容改变后tomcat则自动的进行重启。
步骤:
添加依赖:
<!-- 开发者工具--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <version>2.4.5</version> <optional>true</optional> </dependency>
修改idea自动编译配置:
这里要打勾。
注意在2022以前的版本需要修改Reigstry
Ctrl+Shift+Alt+/ 点击弹出框中Registry…