本文共 1709 字,大约阅读时间需要 5 分钟。
Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎。
Thymeleaf的主要目标是为您的开发工作流程带来优雅的自然模板 -HTML可以在浏览器中正确显示,也可以作为静态原型工作,从而可以在开发团队中加强协作。
Thymeleaf拥有用于Spring Framework的模块,与您喜欢的工具的大量集成以及插入您自己的功能的能力,对于现代HTML5 JVM Web开发而言,Thymeleaf是理想的选择-尽管它可以做很多事情。
JDK 1.8+
thymeleaf:
spring initializr:
- 默认创建了 demo 项目
- Dependencies:添加 String Web
GENERATE
- resources/static 文件夹:存放静态资源,浏览器可直接访问;如:static 下创建 static.html ,可输入 http://localhost:8085/static.html 访问。
- resources/templates文件夹:存放动态资源,即模板页面。
pom.xml
添加 thymeleaf 的依赖org.springframework.boot spring-boot-starter-thymeleaf
application.properties
配置# thymeleafspring.thymeleaf.mode=HTMLspring.thymeleaf.encoding=UTF-8spring.thymeleaf.servlet.content-type=text/htmlspring.thymeleaf.cache=false# springspring.application.name=thymeleaf-demoserver.port=8085
com.example.thymeleafdemo.controller.DemoController.java
package com.example.thymeleafdemo.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class DemoController { @GetMapping("/html/index") public ModelAndView goIndex(ModelAndView mav){ mav.addObject("name", "hello word"); mav.setViewName("test"); return mav; }}
resources\templates\test.html
html标签:
<html lang="en" xmlns:th="http://www.thymeleaf.org">
Test Page Test Page
(1)
(2)
浏览器:输入IP和端口后,默认跳转的页面,例如:http://localhost:8085
static/index.html
static/index.html
不存在,才会查找 templates/index.html
转载地址:http://wznws.baihongyu.com/