大纲

(1)在pom.xml中引入freemarker;

(2)如何关闭freemarker缓存

(3)编写模板文件.ftl

(4)编写访问文件的controller

在pom.xml中引入freemarker

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

如何关闭freemarker缓存

########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.suffix=.ftl
#spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved

编写模板文件.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.v.2</h1>
<p>${hello}</p>
</body>
</html>

编写访问文件的controller

@RequestMapping("/helloFtl")
public String helloFtl(Map<String,Object> map){
map.put("hello","from TemplateController.helloFtl");
return "/helloFtl";
}

代码如下:

Controller类

package com.kfit;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; /**
* 注意:
* 1.在Thymeleaf 模板文件中,标签是需要闭合的,3.0之前是需要闭合的
* 2. thymeleaf 3.0+ 是可以不强制要求闭合的。
*
* 3. 支持同时使用多个模板引擎,比如:thymeleaf和freemarker 可以并存。
* */
@Controller
@RequestMapping("/templates")
public class TemplatesController { /**
* 映射地址是:/templates/hello
* @return
*/
@RequestMapping("/hello")
public String hello(Map<String,Object> map){
//返回的是ModelAndView对象;
// ModelAndView mv = new ModelAndView("hello");
// return mv;
map.put("name","Andy");
return "hello";
} @RequestMapping("/helloFtl")
public String helloFtl(Map<String,Object> map){
map.put("name","Andy");
return "helloFtl";
} }

模板

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
hello ${name} </body>
</html>

配置

########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.cache=false
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
spring.freemarker.suffix=.ftl
#spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved

Spring boot之使用freemarker的更多相关文章

  1. spring boot ----> 常用模板freemarker和thymeleaf

    ===========================freemarker=================================== freemarker 官网:https://freem ...

  2. Spring Boot + Bootstrap + jQuery + Freemarker

    Spring Boot + Bootstrap + jQuery + Freemarker 原文地址:http://qilu.me/post/tech/2018-03-18 最近在写一些Web的东西, ...

  3. Spring Boot使用模板freemarker【从零开始学Spring Boot(转)

    视频&交流平台: à SpringBoot网易云课堂视频 http://study.163.com/course/introduction.htm?courseId=1004329008 à  ...

  4. spring boot中使用freemarker

    在做Java web开发领域,web模板引擎主要有jsp.freemarker.velocity,其中freemarker是我们常用的一种,在spring boot中使用的freemarker的方法可 ...

  5. Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎

    前面几篇介绍了返回json数据提供良好的RESTful api,下面我们介绍如何把处理完的数据渲染到页面上. Spring Boot 使用模板引擎 Spring Boot 推荐使用Thymeleaf. ...

  6. Spring Boot + Freemarker多语言国际化的实现

    最近在写一些Web的东西,技术上采用了Spring Boot + Bootstrap + jQuery + Freemarker.过程中查了大量的资料,也感受到了前端技术的分裂,每种东西都有N种实现, ...

  7. Spring Boot 揭秘与实战(七) 实用技术篇 - FreeMarker 模板引擎

    文章目录 1. FreeMaker 代替 JSP 作为页面渲染 2. 生成静态文件 3. 扩展阅读 4. 源代码 Spring Boot 提供了很多模板引擎的支持,例如 FreeMarker.Thym ...

  8. Spring Boot Freemarker特别篇之contextPath【从零开始学Spring Boot】(转)

    需求缘起:有人在群里@我:请教群主大神一个问题,spring boot  + freemarker 怎么获取contextPath 头疼死我了,网上没一个靠谱的 .我就看看之前博客中的 [Spring ...

  9. SpringBoot系列:Spring Boot使用模板引擎FreeMarker

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

随机推荐

  1. spring boot本地开发与docker容器化部署的差异

    spring boot本地开发与docker容器化部署的差异: 1. 文件路径及文件名区别大小写: 本地开发环境为windows操作系统,是忽略大小写的,但容器中区分大小写 2. docker中的容器 ...

  2. 对ArrayList中的Person对象按照先年龄从大到小,相同年龄的再按照姓名(姓名是英文的)的字母顺序进行排序.

    ListDemo2.java ----------------- package com.fs.test; import java.util.ArrayList; import java.util.C ...

  3. 使用transform属性和animation属性制作跳动的心

    transform属性允许我们对元素进行旋转.缩放.移动和倾斜: animation属性允许我们对元素实现一些动画效果: 跳动的心源码 <!DOCTYPE html> <html l ...

  4. 解决 webpack 打包文件体积过大

    webpack 把我们所有的文件都打包成一个 JS 文件,这样即使你是小项目,打包后的文件也会非常大.下面就来讲下如何从多个方面进行优化. 去除不必要的插件 刚开始用 webpack 的时候,开发环境 ...

  5. 用原生js来写一个swiper滑块插件

        是不是有点印象了,没错,他的最基本的用法就是左右滑动,插件使用者只需要写几行简单的html和js即可实现一个简单滑动效果,不过你完全可以组合各种元素来适应不同的场景. 当然插件我已经写好了,咱 ...

  6. k-means 非监督学习聚类算法

    非监督学习 非监督学习没有历史样本数据和标签,直接对数据分析或得结果. k-means 使用 >>> from sklearn.cluster import KMeans >& ...

  7. css 规范标签

    页头:header 登录条:loginBar 标志:logo 侧栏:sideBar 广告:banner 导航:nav 子导航:subNav 菜单:menu 子菜单:subMenu 搜索:search ...

  8. SpringMVC基础03——常用注解之@RequestMapping

    1.用法 SpringMVC使用@RequestMapping注解,为控制器指定可以处理哪些URL请求,并且可以指定处理请求的类型(POST/GET),如果@RequestMapping没有指定请求的 ...

  9. kubeDNS workflow(service registration and discovery)

    Service discovery In k8s, we usually will more than make an application instance, and also the corre ...

  10. 使用TensorFlow玩GTA5

    小白学TensorFlow(一) tensorflow安装 在安装之前,您必须选择以下类型的TensorFlow之一来安装: TensorFlow仅支持CPU支​​持.如果您的系统没有NVIDIA®G ...