FreeMarker 是一款模板引擎:即一种基于模板和要改变的数据,并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。
FreeMarker是一个基 于Java的开发包和类库的一种将模板和数据进行整合并输出文本的通用工具,FreeMarker实现页面静态化的原理是:将页面中所需要的样式写入到 FreeMarker模板文件中,然后将页面所需要的数据进行动态绑定并放入到Map中,然后通过FreeMarker的模板解析类process()方 法完成静态页面的生成。其工作原理如图2-1所示。
模板 + 数据模型 = 输出实验目标:实现freemaker展现后台变量
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springboot-demo</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>freemaker</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency></dependencies></project>
设置一个共享变量app
package com.et.freemaker.config;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import freemarker.template.TemplateModelException;import javax.annotation.PostConstruct;public class FreemarkerConfiguration {//Configurationprivate freemarker.template.Configuration configuration;public void configuration() throws TemplateModelException {// add globe variablethis.configuration.setSharedVariable("app", "Spring Boot");}}
设置一个title变量
package com.et.freemaker.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.servlet.ModelAndView;import java.util.HashMap;import java.util.Map;public class HelloWorldController {public Map<String, Object> showHelloWorld(){Map<String, Object> map = new HashMap<>();map.put("msg", "HelloWorld");return map;}public ModelAndView index () {ModelAndView modelAndView = new ModelAndView("index/index");// add title to ModelmodelAndView.addObject("title", "Freemarker");return modelAndView;}}
放在resource/templates/index下面
<html><head><meta charset="UTF-8"><title>Freemarker</title></head><body>Hello ${title}!${app}</body></html>
server:port: 8088spring:freemarker:enabled: truecache: false# Content Typecontent-type: text/htmlcharset: utf-8suffix: .ftlrequest-context-attribute: requestexpose-request-attributes: falseexpose-session-attributes: falseallow-request-override: trueallow-session-override: trueexpose-spring-macro-helpers: truecheck-template-location: trueprefer-file-system-access: truetemplate-loader-path:- classpath:/templates/settings:datetime_format: yyyy-MM-dd HH:mm:sstemplate_update_delay: 30mdefault_encoding: utf-8
以上只是一些关键代码,所有代码请参见下面代码仓库
https://github.com/Harries/springboot-demo
启动springboot应用
http://127.0.0.1:8088/,返回内容
Hello Freemarker!Spring Boothttps://docs.spring.io/spring-framework/reference/web/webmvc-view/mvc-freemarker.html
http://www.liuhaihua.cn/archives/710629.html
原文作者:HBLOG;如有侵权,请联系管理员删除。