第一步pom中:

<!-- 引入 thymeleaf 模板依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

第二步 aplication.properties 中:

############################################################
#
# thymeleaf \u9759\u6001\u8d44\u6e90\u914d\u7f6e
#   同样这里也是 需要重新建一个文件夹放在templates下的文件

#  thymeleaf与freemarkerController模板用一个就可以,没必要用两个
############################################################
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
# \u5173\u95ed\u7f13\u5b58, \u5373\u65f6\u5237\u65b0, \u4e0a\u7ebf\u751f\u4ea7\u73af\u5883\u9700\u8981\u6539\u4e3atrue
spring.thymeleaf.cache=false   //这里在上线的时候要改为true

#\u8bbe\u5b9a\u9759\u6001\u6587\u4ef6\u8def\u5f84\uff0cjs,css\u7b49
spring.mvc.static-path-pattern=/static/**   这个默认本来就是这个   还有动态默认在public中

第三步前端:

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>

<!-- <script th:src="@{/static/js/test.js}"></script> -->

</head>
<body>

<div>
用户姓名:<input th:id="${user.name}" th:name="${user.name}" th:value="${user.name}"/>
<br/>
用户年龄:<input th:value="${user.age}"/>
<br/>
用户生日:<input th:value="${user.birthday}"/>
<br/>
用户生日:<input th:value="${#dates.format(user.birthday, 'yyyy-MM-dd')}"/>
<br/>
</div>

<br/>

<div th:object="${user}">
用户姓名:<input th:id="*{name}" th:name="*{name}" th:value="*{name}"/>
<br/>
用户年龄:<input th:value="*{age}"/>
<br/>
用户生日:<input th:value="*{#dates.format(birthday, 'yyyy-MM-dd hh:mm:ss')}"/>
<br/>
</div>

<br/>

text 与 utext :<br/>
<span th:text="${user.desc}">abc</span>
<br/>
<span th:utext="${user.desc}">abc</span>
<br/>
<br/>

URL:<br/>
<a href="" th:href="@{http://www.imooc.com}">网站地址</a>
<br/>

<br/>
<form th:action="@{/th/postform}" th:object="${user}" method="post" th:method="post">
<input type="text" th:field="*{name}"/>
<input type="text" th:field="*{age}"/>
<input type="submit"/>
</form>
<br/>

<br/>
<div th:if="${user.age} == 18">十八岁的天空</div>
<div th:if="${user.age} gt 18">你老了</div>
<div th:if="${user.age} lt 18">你很年轻</div>
<div th:if="${user.age} ge 18">大于等于</div>
<div th:if="${user.age} le 18">小于等于</div>
<br/>

<br/>
<select>
<option >选择框</option>
<option th:selected="${user.name eq 'lee'}">lee</option>
<option th:selected="${user.name eq 'imooc'}">imooc</option>
<option th:selected="${user.name eq 'LeeCX'}">LeeCX</option>
</select>
<br/>

<br/>
<table>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>年龄备注</th>
<th>生日</th>
</tr>
<tr th:each="person:${userList}">
<td th:text="${person.name}"></td>
<td th:text="${person.age}"></td>
<td th:text="${person.age gt 18} ? 你老了 : 你很年轻">18岁</td>
<td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd hh:mm:ss')}"></td>
</tr>
</table>
<br/>

<br/>
<div th:switch="${user.name}">
<p th:case="'lee'">lee</p>
<p th:case="#{roles.manager}">普通管理员</p>
<p th:case="#{roles.superadmin}">超级管理员</p>
<p th:case="*">其他用户</p>
</div>
<br/>

</body>
</html>

四步:controller中

package cn.itcast.springboot.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.itcast.springboot.pojo.User;

@Controller
@RequestMapping("th")
public class ThymeleafController {

@RequestMapping("/index")
public String index(ModelMap map) {
map.addAttribute("name", "thymeleaf-imooc");
return "thymeleaf/index";
}

@RequestMapping("center")
public String center() {
return "thymeleaf/center/center";
}

@RequestMapping("test")
public String test(ModelMap map) {

User u = new User();
u.setName("superadmin");
u.setAge(10);
u.setPassword("123465");
u.setBirthday(new Date());
u.setDesc("<font color='green'><b>hello imooc</b></font>");

map.addAttribute("user", u);

User u1 = new User();
u1.setAge(19);
u1.setName("imooc");
u1.setPassword("123456");
u1.setBirthday(new Date());

User u2 = new User();
u2.setAge(17);
u2.setName("LeeCX");
u2.setPassword("123456");
u2.setBirthday(new Date());

List<User> userList = new ArrayList<>();
userList.add(u);
userList.add(u1);
userList.add(u2);

map.addAttribute("userList", userList);

return "thymeleaf/test";
}

@PostMapping("postform")
public String postform(User u) {

System.out.println("姓名:" + u.getName());
System.out.println("年龄:" + u.getAge());

return "redirect:/th/test";
}

@RequestMapping("showerror")
public String showerror(User u) {

int a = 1 / 0;

return "redirect:/th/test";
}
}

细节用法可参考项目注释.

springboot 引入 thymeleaf 模板的更多相关文章

  1. springboot整合Thymeleaf模板引擎

    引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...

  2. Springboot整合thymeleaf模板

    Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...

  3. SpringBoot 之Thymeleaf模板.

    一.前言 Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1.JSP 最明显的问题在于它看起来像HTML或X ...

  4. springboot引入thymeleaf

    springboot引入thymeleaf 1.Thymeleaf使用 @ConfigurationProperties(prefix = "spring.thymeleaf") ...

  5. SpringBoot(二)thymeleaf模板的引入

    接着上一次的配置 1.在pom文件中添加thymeleaf模板的引入, <dependency> <groupId>org.springframework.boot</g ...

  6. Springboot与Thymeleaf模板引擎整合基础教程(附源码)

    前言 由于在开发My Blog项目时使用了大量的技术整合,针对于部分框架的使用和整合的流程没有做详细的介绍和记录,导致有些朋友用起来有些吃力,因此打算在接下来的时间里做一些基础整合的介绍,当然,可能也 ...

  7. (二)springboot整合thymeleaf模板

    在我们平时的开发中,用了很久的jsp作view显示层,但是标签库和JSP缺乏良好格式的一个副作用就是它很少能够与其产生的HTML类似.所以,在Web浏览器或HTML编辑器中查看未经渲染的JSP模板是非 ...

  8. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

  9. SpringBoot系列——Thymeleaf模板

    前言 thymeleaf是springboot官方推荐使用的java模板引擎,在springboot的参考指南里的第28.1.10 Template Engines中介绍并推荐使用thymeleaf, ...

随机推荐

  1. vue属性

    1. 图片地址: data:{ url:"https://www.baidu.com/img/bd_logo1.png"}, <img v-bind:src="ur ...

  2. Javascript-关于null、undefined、空字符串的区分

    一.分别判断 var a=null; //var a=undefined; //var a=''; //var a='DD'; if(!a&&typeof a == 'object') ...

  3. 封装MemoryCache

    一.定义一个缓存接口IChace using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  4. Chapter2:Qt5模板库,工具类及控件

    2.1 字符串类 QString类保存16位Unicode值,提供了丰富的操作,查询和转换等函数.  (1):QString提供了一个二元的"+"操作符用于组合两个字符串  (2) ...

  5. linux 批量替换

    sed -i "s/新内容/原内容/g" `ls *.html` sed -i "s/新内容/原内容/g/g" `ls *.php` sed -i " ...

  6. ubuntu 16.04在真实机安装后的静态ip的配置

    nssa-sensor1@nssa-sensor1:~$ vim /etc/network/interfaces 以下是编辑文件的内容# interfaces(5) file used by ifup ...

  7. “javac”不是内部或外部命令的解决办法(JDK1.5 ~ JDK10)

    配置环境变量:计算机 -> 属性 -> 高级系统设置 -> 环境变量 JDK9以后版本(9-10): JAVA_HOME D:\Java\jdk-10 //jdk所在目录 JRE_H ...

  8. Mac下RabbitMQ安装和在Java client端的使用

    安装: 1.使用homebrew下载rabbitMQ: brew install rabbitmq 执行结果如下: Updating Homebrew... ==> Auto-updated H ...

  9. 百度 OCR API 的使用以及与 Tesseract 的简单对比

    目录 百度 OCR API 初探 用 Python 调用百度 OCR API 与 Tesseract 的简单对比 百度 OCR API 初探 近日得知百度在其 APIStore 上开放了 OCR 的 ...

  10. 性能测试day01_性能基本概念

    其实第一次接触性能是15年的时候,懵懵懂懂的被领导拉去做第一次做性能压测,如今有机会重新听一下云层大大讲解性能,于是打算以此博客记录下整个学习的过程,如若有不同意见者可以在下面留言指出,也欢迎大家一起 ...