170703、springboot编程之模板使用(thymeleaf、freemarker)
官方不推荐集成jsp,关于使用jsp模板我这里就不赘述,如果有需要的,请自行百度!
thymeleaf的使用
1、在pom中增加thymeleaf支持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
注:Thymeleaf默认是有缓存的,当然不是我们需要的,在配置文件中可以关闭缓存
2、application.properties配置
####thymeleaf模板使用
##关闭thymeleaf缓存
spring.thymeleaf.cache=false
3、编写模板文件helloHtml.html
首先在resources文件下创建templates,然后在templates创建helloHtml.html文件
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1 th:inline="text">Hello World!</h1>
<p th:text="${hello}"></p>
</body>
</html>
4、编写TemplateController.java控制器类
package com.rick.apps.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.Map; /**
* Desc : 模板测试
* User : RICK
* Time : 2017/8/22 13:33
*/
@Controller
public class TemplateController {
/**
* 返回html模板.
*/
@RequestMapping("/helloHtml")
public String helloHtml(Map<String,Object> map){
map.put("hello","Hello Spring Boot");
return"/helloHtml";
}
}
5、启动项目,访问http://localhost:8080/helloHtml

使用freemarker
1、在pom中增加freemarker支持
<!--freemarker-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2、编写TemplateController.java控制器类
/**
* 返回freemarker html模板.
*/
@RequestMapping("/helloFtl")
public String helloFtl(Map<String,Object> map){
map.put("hello","Hello freemarker");
return"/helloFtl";
}
3、创建helloFtl.ftl
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<p>${hello}</p>
</body>
</html>
4、启动访问http://localhost:8080/helloFtl

另外需要注意的是:thymeleaf和freemarker是可以同时存在的!
项目清单:

170703、springboot编程之模板使用(thymeleaf、freemarker)的更多相关文章
- springboot的推荐模板引擎-Thymeleaf
1)添加对themeleaf的支持的依赖 <!--Thymeleaf--> <dependency> <groupId>org.springframework.bo ...
- springboot-10-前端页面整合, thymeleaf, freemarker, jsp 模板使用
springboot 中不建议使用jsp作为页面展示, 怎么使用可以看: http://412887952-qq-com.iteye.com/blog/2292471 关于什么是thymeleaf, ...
- SpringBoot系列:Spring Boot使用模板引擎Thymeleaf
一.Java模板引擎 模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档. 在jav ...
- SpringBoot入门系列(四)整合模板引擎Thymeleaf
前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/ ...
- SpringBoot:模板引擎 thymeleaf、ContentNegotiatingViewResolver、格式化转换器
目录 模板引擎 thymeleaf ContentNegotiatingViewResolver 格式化转换器 模板引擎 thymeleaf.ContentNegotiatingViewResolve ...
- springboot入门_模板
springboot中已经不推荐使用jsp,而是推荐使用模板,如freemarker,thymeleaf等,本文记录在sprigboot中使用模板. 创建一个maven的springboot工程, f ...
- SpringBoot系列: Pebble模板引擎
===============================Java 模板引擎选择===============================SpringBoot Starter项目向导中可选的J ...
- Spring Boot (四)模板引擎Thymeleaf集成
一.Thymeleaf介绍 Thymeleaf是一种Java XML / XHTML / HTML5模板引擎,可以在Web和非Web环境中使用.它更适合在基于MVC的Web应用程序的视图层提供XHTM ...
- 新一代Java模板引擎Thymeleaf
新一代Java模板引擎Thymeleaf-spring,thymeleaf,springboot,java 相关文章-天码营 https://www.tianmaying.com/tutorial/u ...
随机推荐
- glob 遍历函数
例子 1 <?php print_r(glob("*.txt")); ?> 输出类似: Array ( [0] => target.txt [1] => s ...
- 【转】【C++】C++ 中的线程、锁和条件变量
线程 类std::thread代表一个可执行线程,使用时必须包含头文件<thread>.std::thread可以和普通函数,匿名函数和仿函数(一个实现了operator()函数的类)一同 ...
- 在 C++ 程序中只使用 const 常量而不使用宏常量
在 C++ 程序中只使用 const 常量而不使用宏常量,即 const 常量完 全取代宏常量. #include <iostream> /* run this program using ...
- HBase源代码分析之MemStore的flush发起时机、推断条件等详情
前面的几篇文章.我们具体介绍了HBase中HRegion上MemStore的flsuh流程,以及HRegionServer上MemStore的flush处理流程.那么,flush究竟是在什么情况下触发 ...
- Oracle过程及函数的参数模式详解
一.In.out.in out模式 在Oracle中过程与函数都可以有参数,参数的类型可以指定为in.out.in out三种模式. 三种参数的具体说明,如下图所示: (1)in模式 in模式是引用传 ...
- 【Deep learning】NLP
http://www.tuicool.com/articles/EvaQJnJ http://cs224d.stanford.edu/syllabus.html
- JS中函数声明与函数表达式的异同
相同点 注:函数声明和函数表达式的相同点包括但不限于以下几点 函数是一个值,所以和其他值一样,函数也可以进行被输出.被赋值.作为参数传给其他函数等相关操作,不管函数是以什么方式被定义的,当然和其他值的 ...
- 2011年冬斯坦福大学公开课 iOS应用开发教程学习笔记(第三课)
第二课名称是:Objective-C 回顾上节课的内容: 创建了单个MVC模式的项目 显示项目的各个文件,显示或隐藏导航,Assistant Editor, Console, Object Libra ...
- Linux环境PHP5.5以上连接SqlServer2008
linux版本:64位CentOS 6.4 Nginx版本:nginx1.8.0 php版本:php5.5.28 Sqlserver版本:2008 FreeTDS版本:0.95 关于Linux环境安装 ...
- 处理unsigned相减错误(BIGINT UNSIGNED value is out of range)
mysql 当两个字段想减时,如果其中一个或两个字段的类型的unsigned无签名类型,如果想减的值小于0则会报错(BIGINT UNSIGNED value is out of range) 测试: ...