Thymeleaf模板
Thymeleaf就是html页面
导入pom依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-thymeleaf</artifactId> 4 </dependency>
Spring Boot官方文档建议在开发时将缓 存关闭,那就在application.yml文件中加入:
正式环境还是要将缓存开启的
实体类
User
1 package com.javaxl.springboot01.entity; 2 3 import lombok.Data; 4 5 /** 6 * @author XuFanQi 7 * @site 8 * @company 9 * @create 2019-11-26 15:37 10 */ 11 @Data 12 public class User { 13 private Integer uid; 14 private String uname; 15 private String pwd; 16 17 public User(Integer uid, String uname, String pwd) { 18 this.uid = uid; 19 this.uname = uname; 20 this.pwd = pwd; 21 } 22 23 public User() { 24 } 25 }
Controller层
userController
1 package com.javaxl.springboot01.controller; 2 3 import com.javaxl.springboot01.entity.User; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 7 import javax.servlet.http.HttpServletRequest; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 /** 12 * @author XuFanQi 13 * @site 14 * @company 15 * @create 2019-11-26 15:39 16 */ 17 @Controller 18 @RequestMapping("/thymeleaf") 19 public class userController { 20 21 @RequestMapping("/list") 22 public String hello(HttpServletRequest request){ 23 /** 24 * 1.获取单个值 25 * 2.能够在html页面竞争遍历展示 26 * 3.如何在HTML页面转义代码块 27 */ 28 request.setAttribute("msg","传输单个字符串! ! !"); 29 List<User> userList = new ArrayList<>(); 30 userList.add(new User(1,"zs","123456")); 31 userList.add(new User(2,"ls","1234567")); 32 userList.add(new User(3,"ww","1234568")); 33 request.setAttribute("userList",userList); 34 request.setAttribute("htmlStr","<span style='color:red'>转义html代码块</span>"); 35 36 return "list"; 37 } 38 }
list.html页面
(注意:
<html xmlns:th="http://www.thymeleaf.org">
)
1 <!DOCTYPE html> 2 <html xmlns:th="http://www.thymeleaf.org"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>thymeleaf模板介绍</title> 6 </head> 7 <body> 8 <div th:text = "${msg}"></div> 9 <table width="60%" border="1"> 10 <tr> 11 <td>ID</td> 12 <td>Uname</td> 13 <td>Pwd</td> 14 </tr> 15 <tr th:each="u : ${userList}"> 16 <td th:text="${u.uid}"></td> 17 <td th:text="${u.uname}"></td> 18 <td th:text="${u.pwd}"></td> 19 </tr> 20 </table> 21 <div th:utext="${htmlStr}"></div> 22 23 </body> 24 </html>
浏览器访问
Freemarker模板
首先导入pom依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-freemarker</artifactId> 4 </dependency> 5 6 <!--可以不加,但是做项目的时候可能会用--> 7 <resources> 8 <!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题--> 9 <resource> 10 <directory>src/main/java</directory> 11 <includes> 12 <include>**/*.xml</include> 13 </includes> 14 </resource> 15 <!--freemarker模板也读取需要注释标红地方--> 16 <resource> 17 <directory>src/main/resources</directory> 18 <includes> 19 <!--<include>*.properties</include>--> 20 <!--<include>*.xml</include>--> 21 <!--<include>*.yml</include>--> 22 </includes> 23 </resource> 24 </resources>
配置application.yml
1 server: 2 servlet: 3 context-path: /spr 4 port: 80 5 6 7 user: 8 uname: zs 9 pwd: 123456 10 age: 18 11 sex: "男" 12 addr: "北京" 13 spring: 14 thymeleaf: 15 cache: false 16 freemarker: 17 # 设置模板后缀名 18 suffix: .ftl 19 # 设置文档类型 20 content-type: text/html 21 # 设置页面编码格式 22 charset: UTF-8 23 # 设置页面缓存 24 cache: false 25 # 设置ftl文件路径,默认是/templates,为演示效果添加role 26 template-loader-path: classpath:/templates/freemarker 27 mvc: 28 static-path-pattern: /static/**
配置freemarker.ftl
前台页面
list.ftl
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 <h2>取值</h2> 9 <h3>提供默认值</h3> 10 welcome 【${name!'未知'}】 to freemarker! 11 12 <h3>exists用在逻辑判断</h3> 13 <#if name?exists> 14 ${name} 15 </#if> 16 17 <h2>条件</h2> 18 <#if sex=='girl'> 19 女 20 <#elseif sex=='boy'> 21 男 22 <#else> 23 保密 24 </#if> 25 26 <h2>循环</h2> 27 <table border="1px" width="600px"> 28 <thead> 29 <tr> 30 <td>ID</td> 31 <td>角色名</td> 32 <td>描述</td> 33 </tr> 34 </thead> 35 <tbody> 36 <#list roles as role> 37 <tr> 38 <td>${role.rid}</td> 39 <td>${role.roleName}</td> 40 <td>${role.desc}</td> 41 </tr> 42 </#list> 43 </tbody> 44 </table> 45 46 <h2>include</h2> 47 <#include 'foot.ftl'> 48 49 <h2>局部变量(assign)/全局变量(global)</h2> 50 <#assign ctx1> 51 ${springMacroRequestContext.contextPath} 52 </#assign> 53 54 <#global ctx2> 55 ${springMacroRequestContext.contextPath} 56 </#global> 57 58 ss 59 ${ctx1}和${ctx2} 60 ss 61 62 </body> 63 </html>
foot.ftl
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 版权 9 </body> 10 </html>
实体类
Role
1 package com.javaxl.springboot01.entity; 2 3 import lombok.Data; 4 5 /** 6 * @author XuFanQi 7 * @site 8 * @company 9 * @create 2019-11-26 16:53 10 */ 11 @Data 12 public class Role { 13 private Integer rid; 14 private String roleName; 15 private String desc; 16 17 public Role(Integer rid, String roleName, String desc) { 18 this.rid = rid; 19 this.roleName = roleName; 20 this.desc = desc; 21 } 22 23 public Role() { 24 } 25 26 // public Integer getRid() { 27 // return rid; 28 // } 29 // 30 // public void setRid(Integer rid) { 31 // this.rid = rid; 32 // } 33 // 34 // public String getRoleName() { 35 // return roleName; 36 // } 37 // 38 // public void setRoleName(String roleName) { 39 // this.roleName = roleName; 40 // } 41 // 42 // public String getDesc() { 43 // return desc; 44 // } 45 // 46 // public void setDesc(String desc) { 47 // this.desc = desc; 48 // } 49 }
Controller层
RoleController
1 package com.javaxl.springboot01.controller; 2 3 import com.javaxl.springboot01.entity.Role; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.servlet.ModelAndView; 7 8 import java.util.ArrayList; 9 import java.util.List; 10 11 /** 12 * @author XuFanQi 13 * @site 14 * @company 15 * @create 2019-11-26 16:49 16 */ 17 @Controller 18 @RequestMapping("/freemarker") 19 public class RoleController { 20 @RequestMapping("/role/list") 21 public ModelAndView roleList(){ 22 ModelAndView mav = new ModelAndView(); 23 mav.setViewName("/list"); 24 25 mav.addObject("name",null); 26 mav.addObject("sex","gay"); 27 List list = new ArrayList(); 28 list.add(new Role(1,"老师","教书育人")); 29 list.add(new Role(2,"学生","知识改变命运")); 30 mav.addObject("roles",list); 31 32 return mav; 33 } 34 35 @RequestMapping("toLogin") 36 public String toLogin(){ 37 return "login"; 38 } 39 }
浏览器访问