1、SpringBoot支持的模板引擎html
一、Thymeleaf(官方推荐)java
二、FreeMarkerweb
三、Groovyspring
四、mustache后端
SpringBoot为何不推荐使用JSP呢?缓存
一、JSP对页面的侵入性较强。app
二、web容器版本的的管理问题。spring-boot
2、关于thymeleaf测试
作到了先后端的完美分离ui
3、实现MVC
不一样的包存放的文件以下图所示:
一、引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
二、编写controller
/* * Copyright (c) 2018 solidwang. All Rights Reserved */ package com.solid4j.controller; import com.solid4j.bean.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import java.util.ArrayList; import java.util.List; /** * @author: solidwang * @date:2018/4/19 上午10:35 */ @Controller @RequestMapping("/thymeleaf") public class ThymeleafController { @RequestMapping("") public ModelAndView index(){ List<User> userList = new ArrayList<User>(); User user1 = new User("solidwang", "solidwang@126.com"); User user2 = new User("jobs", "jobs@me.com"); userList.add(user1); userList.add(user2); ModelAndView modelAndView = new ModelAndView("/index"); modelAndView.addObject("userList", userList); return modelAndView; } }
三、模板文件(index.html)
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>learn Resources</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <div> <h1>Thymeleaf测试</h1> <table border="1" cellspacing="1" cellpadding="0"> <tr> <td>姓名</td> <td>passport</td> </tr> <tr th:each="user : ${userList}"> <td th:text="${user.username}">solidwang</td> <td th:text="${user.passport}">solidwang@me.com</td> </tr> </table> </div> </body> </html>
四、测试结果以下:
五、注意事项
若是要模板页面实时刷新,须要配置application.properties文件,spring.thymeleaf.cache=false,若是依然没有生效,能够对html文件进行一次编译便可。
#thymeleaf start spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 #开发时关闭缓存,否则无法看到实时页面 spring.thymeleaf.cache=false #thymeleaf end