【原创文章,转载请注明出处】
103. Spring Boot Freemarker特别篇之contextPath【从零开始学Spring Boot】
最近有好久没有更新博客了,感谢小伙伴的默默支持,不知道是谁又打赏了我一个小红包,谢谢。
今天我们讲讲怎么在Spring Boot中使用模板引擎freemarker,先看看今天的大纲:
(2) 新建spring-boot-freemarker工程;
(3) 在pom.xml引入相关依赖;
(4) 编写启动类;
(5) 编写模板文件hello.ftl;
(6) 编写访问类HelloController;
(7) 测试;
(8) freemarker配置;
(9) freemarker常用语法;
(10) freemarker layout 布局
(1) freemarker介绍;
FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。
(2) 新建spring-boot-freeMarker工程;
我们新建一个maven工程,取名为:spring-boot-freemarker
(3) 在pom.xml引入相关依赖;
这里使用freeMarker需要引入相关依赖包:spring-boot-starter-freemarker,
<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"> <modelVersion>4.0.0</modelVersion> <groupId>com.kfit</groupId> <artifactId>spring-boot-velocity</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-velocity</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- jdk版本号,angel在这里使用1.8,大家修改为大家本地配置的jdk版本号即可 --> <java.version>1.8</java.version> </properties> <!-- spring boot 父节点依赖, 引入这个之后相关的引入就不需要添加version配置, spring boot会自动选择最合适的版本进行添加。 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version><!-- 1.4.1.RELEASE , 1.3.3.RELEASE--> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!-- spring boot web支持:mvc,aop... --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 引入freeMarker的依赖包. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> </dependencies> </project>
(4) 编写启动类;
启动类没有什么特别之处,不过多介绍,请看代码:
package com.kfit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * * @author Angel --守护天使 * @version v.0.1 * @date 2016年10月4日 */ @SpringBootApplication public class App { publicstaticvoid main(String[] args) { SpringApplication.run(App.class, args); } }
(5) 编写模板文件hello.ftl;
编写一个hello.ftl文件,此文件的路径在src/main/resources/templates下,其中hello.ftl文件的内容如下:
<html> <body> welcome ${name} to freemarker! </body> </html>
(6) 编写访问类HelloController;
有了模板文件之后,我们需要有个Controller控制类,能够访问到hello.ftl文件,这里也很简单,具体看如下代码:
package com.kfit.demo.web; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * 测试velocity; * @author Angel --守护天使 * @version v.0.1 * @date 2016年10月4日 */ @Controller public class HelloController { @RequestMapping("/hello") public String hello(Map<String,Object> map){ map.put("name", "[Angel -- 守护天使]"); return "hello"; } }
(7) 测试;
好了,到这里,我们就可以启动我们的程序进行测试了,访问地址:
http://127.0.0.1:8080/hello ,如果你在浏览器中看到如下信息:
welcome [Angel -- 守护天使] to freemarker!
那么说明你的demo ok 了。
(8) freemarker配置;
在spring boot的application.properties属性文件中为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
(9) freemarker常用语法;
freemarker的语法并不是本节的重点,这里还是简单的介绍下几个常用的if else,list;
首先我们改造下HelloController的hello方法
@RequestMapping("/hello") public String hello(Map<String,Object> map){ map.put("name", "[Angel -- 守护天使]"); map.put("gender",1);//gender:性别,1:男;0:女; List<Map<String,Object>> friends =new ArrayList<Map<String,Object>>(); Map<String,Object> friend = new HashMap<String,Object>(); friend.put("name", "张三"); friend.put("age", 20); friends.add(friend); friend = new HashMap<String,Object>(); friend.put("name", "李四"); friend.put("age", 22); friends.add(friend); map.put("friends", friends); return "hello"; }
这里我们返回了gender和friends的列表;
接下来我们看看怎么在freemarker进行展示呢?
<!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> <p> welcome ${name} to freemarker! </p> <p>性别: <#if gender==0> 女 <#elseif gender==1> 男 <#else> 保密 </#if> </p> <h4>我的好友:</h4> <#list friends as item> 姓名:${item.name} , 年龄${item.age} <br> </#list> </body> </html>
(10) freemarker layout
freemarker layout主要处理具有相同内容的页面,比如每个网站的header和footer页面。
freemarker 的布局主要常见的两种方式是#import(“文件路径”)和#include(“文件路径”),其中import和include的区别在于,include常用于公共部分的页面,如果要使用<#assign username=“张三”>涉及到内部函数以及变量声明之类的,使用import进行导入,如果在import中的页面含有页面当前将不会进行渲染。 我们编写一个header和footer,其中的header使用include引入,footer页面也使用include引入。(当然freemarker 还有别的布局方式,这里只是介绍一种,请自行学习研究)
header.ftl内容:
<header> This is a header,welcome ${name} to my web site! </header> <hr>
footer.ftl内容:
<hr> <footer> This is a footer,welcome ${name} to my web site! </footer>
修改hello.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> <#include "/header.ftl" > <p> welcome ${name} to freemarker! </p> <p>性别: <#if gender==0> 女 <#elseif gender==1> 男 <#else> 保密 </#if> </p> <h4>我的好友:</h4> <#list friends as item> 姓名:${item.name} , 年龄${item.age} <br> </#list> <#include "/footer.ftl" > </body> </html>
到这里就ok了,我们访问/hello页面,应该会看到如下图的效果:
103. Spring Boot Freemarker特别篇之contextPath【从零开始学Spring Boot】
à悟空学院:https://t.cn/Rg3fKJD
学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!
SpringBoot视频:http://t.cn/A6ZagYTi
Spring Cloud视频:http://t.cn/A6ZagxSR
SpringBoot Shiro视频:http://t.cn/A6Zag7IV
SpringBoot交流平台:https://t.cn/R3QDhU0
SpringData和JPA视频:http://t.cn/A6Zad1OH
SpringSecurity5.0视频:http://t.cn/A6ZadMBe
Sharding-JDBC分库分表实战:http://t.cn/A6ZarrqS
分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr
相关推荐
1.21 使用模板(thymeleaf-freemarker) 1.22 Spring Boot 添加JSP支持 1.23 Spring Boot Servlet 1.24 Spring Boot过滤器、监听器 1.25 Spring Boot 拦截器HandlerInterceptor 1.26 Spring Boot启动加载数据...
spring-boot-starter-freemarker-1.5.4.RELEASE.jar
spring-boot-freemarker 整合源码
Spring Boot 系列教程 freemarker导出word下载
本篇文章主要介绍了Spring Boot使用模板freemarker的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring Boot Documentation 1. About the Documentation 2. Getting Help 3. First Steps 4. Working with Spring Boot 5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics II. ...
spring-boot-starter-freemarker-1.3.3.RELEASE.jar
spring-boot-starter-freemarker-2.0.0.RELEASE
spring boot 2.x 完整版 整合了 spring boot+mybatis+redis +freemarker 实现用户的登录注册
NULL 博文链接:https://412887952-qq-com.iteye.com/blog/2292402
Spring boot + Mybatis + Freemarker
15.什么是 FreeMarker 模板? 16.如何使用 Spring Boot 实现异常处理? 17.您使用了哪些 starter maven 依赖项? 18.什么是 CSRF 攻击? 19.什么是 WebSockets? 20.什么是 AOP? 21.什么是 Apache Kafka? 22.我们...
在这里将通过Freemarker这个模板引擎来实现导出 Word,项目不限于Swing,SSH,SSM,Spring Boot 之类的 ————————————— Spring Boot,Freemarker 编辑好格式的Word文档 1份 将Word文档里需要填充的地方...
使用freeMarker模板和Spring boot后端API接口实现查询用户列表的功能。 http://localhost:8080/ftl/index
使用Spring Boot v. 5. 了解Spring Boot特性 vi. 6. 迁移到生存环境 vii. 7. 高级主题 3. II. 开始 i. 8. Spring Boot介绍 ii. 9. 系统要求 i. 9.1. Servlet容器 iii. 10. Spring Boot安装 i. 10.1. 为Java开发者...
使用Spring boot + FreeMarker 实现代码生成功能,完整案例可以查看 eladmin 项目,教程地址: https://www.ydyno.com/archives/1177.html
- chapter9-2-1:[Spring Boot中使用Dubbo进行服务治理] - chapter9-2-2:[Spring Boot与Dubbo中管理服务依赖] ### Spring Cloud构建微服务架构 由于Spring Cloud偏宏观架构,Spring Boot偏微观细节,内容上越来越...
课程目录 01、Spring Boot之Hello World_高清.mp4 02、spring boot返回json数据_高清.mp4 ... 14、Spring Boot 使用freemarker_标清.flv 15、Spring Boot添加JSP支持_标清.flv 16、Spring Boot集成MyBatis_标清.flv
spring boot + mybaits +freemarker +maven 架构来袭 注解版 spring boot + mybaits +freemarker +maven 架构来袭 注解版
freemarker.jar freemarker架包 freemarker生成excel、word、html、xml简单例子freemarker生成excel、word、html、xml简单例子