官方不推荐集成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)的更多相关文章

  1. springboot的推荐模板引擎-Thymeleaf

    1)添加对themeleaf的支持的依赖 <!--Thymeleaf--> <dependency> <groupId>org.springframework.bo ...

  2. springboot-10-前端页面整合, thymeleaf, freemarker, jsp 模板使用

    springboot 中不建议使用jsp作为页面展示, 怎么使用可以看: http://412887952-qq-com.iteye.com/blog/2292471 关于什么是thymeleaf, ...

  3. SpringBoot系列:Spring Boot使用模板引擎Thymeleaf

    一.Java模板引擎 模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档. 在jav ...

  4. SpringBoot入门系列(四)整合模板引擎Thymeleaf

    前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/ ...

  5. SpringBoot:模板引擎 thymeleaf、ContentNegotiatingViewResolver、格式化转换器

    目录 模板引擎 thymeleaf ContentNegotiatingViewResolver 格式化转换器 模板引擎 thymeleaf.ContentNegotiatingViewResolve ...

  6. springboot入门_模板

    springboot中已经不推荐使用jsp,而是推荐使用模板,如freemarker,thymeleaf等,本文记录在sprigboot中使用模板. 创建一个maven的springboot工程, f ...

  7. SpringBoot系列: Pebble模板引擎

    ===============================Java 模板引擎选择===============================SpringBoot Starter项目向导中可选的J ...

  8. Spring Boot (四)模板引擎Thymeleaf集成

    一.Thymeleaf介绍 Thymeleaf是一种Java XML / XHTML / HTML5模板引擎,可以在Web和非Web环境中使用.它更适合在基于MVC的Web应用程序的视图层提供XHTM ...

  9. 新一代Java模板引擎Thymeleaf

    新一代Java模板引擎Thymeleaf-spring,thymeleaf,springboot,java 相关文章-天码营 https://www.tianmaying.com/tutorial/u ...

随机推荐

  1. thinkphp 点击分类显示分类下的文章(完整)

    控制器 <?php // 本类由系统自动生成,仅供测试用途 class IndexAction extends Action { public function index(){ $cate=M ...

  2. (转)使用AfxGetMainWnd函数的一个心得

    作者:朱金灿 来源:http://blog.csdn.net/clever101/ 使用AfxGetMainWnd函数获取MFC程序中的主框架类指针是一个常用作法.但是你会发现这一做法有时也会失灵.不 ...

  3. (转)学习linux的几本书

    成为一名精通 Linux 程序设计的高级程序员一直是不少朋友孜孜以求的目标.根据中华英才网统计数据,北京地区 Linux 程序员月薪平均为 Windows 程序员的 1.8 倍.Java 程序员的 2 ...

  4. ftp命令行工具如何 连接 非标准21端口(其他端口)的ftp服务器

    windows: step1:ftp命令进入ftp交互环境 step2:ftp>open ip空格port 然后...

  5. fildder教程

    转载地址:写得很不错的fildder教程   http://kb.cnblogs.com/page/130367/ Fiddler的基本介绍 Fiddler的官方网站:  www.fiddler2.c ...

  6. JAVA语言基础内部测试题(50道选择题)

    JAVA语言基础内部测试题 选择题(针对以下题目,请选择最符合题目要求的答案,针对每一道题目,所有答案都选对,则该题得分,所选答案错误或不能选出所有答案,则该题不得分.)(每题2分) 没有注明选择几项 ...

  7. MVC源码

    http://aspnetwebstack.codeplex.com/ MVC源码

  8. Hash表 hash table 又名散列表

    直接进去主题好了. 什么是哈希表? 哈希表(Hash table,也叫散列表),是根据key而直接进行访问的数据结构.也就是说,它通过把key映射到表中一个位置来访问记录,以加快查找的速度.这个映射函 ...

  9. JVM学习(一)

    JVM自身的物理结构:

  10. C# string.Format("{0:C3}", 2)