上一篇给大家介绍了springboot整合freemarker,这一片来继续为大家介绍一种模板thymeleaf。
首先在项目中增添thymeleaf依赖spring-boot-starter-thymeleaf
同时为了解决html严格校验报错的问题,增添依赖nekohtml
pom文件代码如下:
4.0.0 com.dalaoyang springboot_thymeleaf0.0.1-SNAPSHOT jar springboot_thymeleaf springboot_thymeleaf org.springframework.boot spring-boot-starter-parent1.5.10.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-thymeleaforg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-devtoolsruntime org.springframework.boot spring-boot-starter-testtest net.sourceforge.nekohtml nekohtml1.9.15 org.springframework.boot spring-boot-maven-plugin
controller代码大致与freemarker相同,代码如下:
package com.dalaoyang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("/")
public String testThymeleaf(ModelMap modelMap){
modelMap.addAttribute("msg", "Hello dalaoyang , this is thymeleaf");
return "thymeleaf";
}
}
application.properties如下:
##端口号 server.port=8888 ##去除thymeleaf的html严格校验 spring.thymeleaf.mode=LEGACYHTML5 #设定thymeleaf文件路径 默认为src/main/resources/templates spring.freemarker.template-loader-path=classpath:/templates #设定静态文件路径,js,css等 spring.mvc.static-path-pattern=/static/** # 是否开启模板缓存,默认true # 建议在开发时关闭缓存,不然没法看到实时页面 spring.thymeleaf.cache=false # 模板编码 spring.freemarker.charset=UTF-8
html代码如下
thymeleaf
启动项目,访问http://localhost:8888/ 即可看到以下页面,
源码下载 :大老杨码云



