视频&交流平台

à SpringBoot网易云课堂视频

http://study.163.com/course/introduction.htm?courseId=1004329008

à Spring Boot交流平台

http://412887952-qq-com.iteye.com/blog/2321532

【原创文章,转载请注明出处】

103. Spring Boot Freemarker特别篇之contextPath【从零开始学Spring Boot】

最近有好久没有更新博客了,感谢小伙伴的默默支持,不知道是谁又打赏了我一个小红包,谢谢。

今天我们讲讲怎么在Spring Boot中使用模板引擎freemarker,先看看今天的大纲:

写道
(1) 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,

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.kfit</groupId>
  5. <artifactId>spring-boot-velocity</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>spring-boot-velocity</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <!-- jdk版本号,angel在这里使用1.8,大家修改为大家本地配置的jdk版本号即可 -->
  13. <java.version>1.8</java.version>
  14. </properties>
  15. <!--
  16. spring boot 父节点依赖,
  17. 引入这个之后相关的引入就不需要添加version配置,
  18. spring boot会自动选择最合适的版本进行添加。
  19. -->
  20. <parent>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-parent</artifactId>
  23. <version>1.4.1.RELEASE</version><!-- 1.4.1.RELEASE , 1.3.3.RELEASE-->
  24. </parent>
  25. <dependencies>
  26. <dependency>
  27. <groupId>junit</groupId>
  28. <artifactId>junit</artifactId>
  29. <scope>test</scope>
  30. </dependency>
  31. <!-- spring boot web支持:mvc,aop... -->
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-web</artifactId>
  35. </dependency>
  36. <!-- 引入freeMarker的依赖包. -->
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-freemarker</artifactId>
  40. </dependency>
  41. </dependencies>
  42. </project>
<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>
&lt;!--
spring boot 父节点依赖,
引入这个之后相关的引入就不需要添加version配置,
spring boot会自动选择最合适的版本进行添加。
--&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;1.4.1.RELEASE&lt;/version&gt;&lt;!-- 1.4.1.RELEASE , 1.3.3.RELEASE--&gt;
&lt;/parent&gt;

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<scope>test</scope>

</dependency>

    &lt;!-- spring boot web支持:mvc,aop... --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt; &lt;!-- 引入freeMarker的依赖包. --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-freemarker&lt;/artifactId&gt;
&lt;/dependency&gt;

</dependencies>

</project>

(4) 编写启动类;

启动类没有什么特别之处,不过多介绍,请看代码:

  1. package com.kfit;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. *
  6. * @author Angel --守护天使
  7. * @version v.0.1
  8. * @date 2016年10月4日
  9. */
  10. @SpringBootApplication
  11. public class App {
  12. publicstaticvoid main(String[] args) {
  13. SpringApplication.run(App.class, args);
  14. }
  15. }
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文件的内容如下:

  1. <html>
  2. <body>
  3. welcome ${name}  to freemarker!
  4. </body>
  5. </html>
<html>
<body>
welcome ${name} to freemarker!
</body>
</html>

(6) 编写访问类HelloController;

有了模板文件之后,我们需要有个Controller控制类,能够访问到hello.ftl文件,这里也很简单,具体看如下代码:

  1. package com.kfit.demo.web;
  2. import java.util.Map;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. /**
  6. * 测试velocity;
  7. * @author Angel --守护天使
  8. * @version v.0.1
  9. * @date 2016年10月4日
  10. */
  11. @Controller
  12. public class HelloController {
  13. @RequestMapping("/hello")
  14. public String hello(Map<String,Object> map){
  15. map.put("name", "[Angel -- 守护天使]");
  16. return "hello";
  17. }
  18. }
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方法

  1. @RequestMapping("/hello")
  2. public String hello(Map<String,Object> map){
  3. map.put("name", "[Angel -- 守护天使]");
  4. map.put("gender",1);//gender:性别,1:男;0:女;
  5. List<Map<String,Object>> friends =new ArrayList<Map<String,Object>>();
  6. Map<String,Object> friend = new HashMap<String,Object>();
  7. friend.put("name", "张三");
  8. friend.put("age", 20);
  9. friends.add(friend);
  10. friend = new HashMap<String,Object>();
  11. friend.put("name", "李四");
  12. friend.put("age", 22);
  13. friends.add(friend);
  14. map.put("friends", friends);
  15. return "hello";
  16. }
@RequestMapping("/hello")
public String hello(Map<String,Object> map){
map.put("name", "[Angel -- 守护天使]");
map.put("gender",1);//gender:性别,1:男;0:女;
   List&lt;Map&lt;String,Object&gt;&gt; friends =new ArrayList&lt;Map&lt;String,Object&gt;&gt;();
Map&lt;String,Object&gt; friend = new HashMap&lt;String,Object&gt;();
friend.put("name", "张三");
friend.put("age", 20);
friends.add(friend);
friend = new HashMap&lt;String,Object&gt;();
friend.put("name", "李四");
friend.put("age", 22);
friends.add(friend);
map.put("friends", friends);
return "hello";
}</pre>

这里我们返回了gender和friends的列表;

接下来我们看看怎么在freemarker进行展示呢?

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
  3. xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
  4. <head>
  5. <title>Hello World!</title>
  6. </head>
  7. <body>
  8. <p>
  9. welcome ${name}  to freemarker!
  10. </p>
  11. <p>性别:
  12. <#if gender==0>
  13. <#elseif gender==1>
  14. <#else>
  15. 保密
  16. </#if>
  17. </p>
  18. <h4>我的好友:</h4>
  19. <#list friends as item>
  20. 姓名:${item.name} , 年龄${item.age}
  21. <br>
  22. </#list>
  23. </body>
  24. </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>
<p>
welcome ${name} to freemarker!
</p>
   &lt;p&gt;性别:
&lt;#if gender==0&gt;

&lt;#elseif gender==1&gt;

&lt;#else&gt;
保密
&lt;/#if&gt;
&lt;/p&gt; &lt;h4&gt;我的好友:&lt;/h4&gt;
&lt;#list friends as item&gt;
姓名:${item.name} , 年龄${item.age}
&lt;br&gt;
&lt;/#list&gt; &lt;/body&gt;

</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内容:

  1. <header>
  2. This is a header,welcome  ${name} to my web site!
  3. </header>
  4. <hr>
<header>
This is a header,welcome ${name} to my web site!
</header>
<hr>

footer.ftl内容:

  1. <hr>
  2. <footer>
  3. This is a footer,welcome  ${name} to my web site!
  4. </footer>
<hr>
<footer>
This is a footer,welcome ${name} to my web site!
</footer>

修改hello.ftl:

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
  3. xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
  4. <head>
  5. <title>Hello World!</title>
  6. </head>
  7. <body>
  8. <#include "/header.ftl" >
  9. <p>
  10. welcome ${name}  to freemarker!
  11. </p>
  12. <p>性别:
  13. <#if gender==0>
  14. <#elseif gender==1>
  15. <#else>
  16. 保密
  17. </#if>
  18. </p>
  19. <h4>我的好友:</h4>
  20. <#list friends as item>
  21. 姓名:${item.name} , 年龄${item.age}
  22. <br>
  23. </#list>
  24. <#include "/footer.ftl" >
  25. </body>
  26. </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>
    &lt;#include "/header.ftl" &gt;

   &lt;p&gt;
welcome ${name} to freemarker!
&lt;/p&gt; &lt;p&gt;性别:
&lt;#if gender==0&gt;

&lt;#elseif gender==1&gt;

&lt;#else&gt;
保密
&lt;/#if&gt;
&lt;/p&gt; &lt;h4&gt;我的好友:&lt;/h4&gt;
&lt;#list friends as item&gt;
姓名:${item.name} , 年龄${item.age}
&lt;br&gt;
&lt;/#list&gt; &lt;#include "/footer.ftl" &gt;
&lt;/body&gt;

</html>

到这里就ok了,我们访问/hello页面,应该会看到如下图的效果:

103. Spring Boot Freemarker特别篇之contextPath【从零开始学Spring Boot】

视频&交流平台

à SpringBoot网易云课堂视频

http://study.163.com/course/introduction.htm?courseId=1004329008

à Spring Boot交流平台

http://412887952-qq-com.iteye.com/blog/2321532

Spring Boot使用模板freemarker【从零开始学Spring Boot(转)的更多相关文章

  1. 72.spring boot讨论群【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 如果您碰到什么问题,您可以加群进行探讨,在群里有加入的都是Spring Boot志同道合的朋友: Spring Boot QQ交流群:193341 ...

  2. 59. Spring Boot Validator校验【从零开始学Spring Boot】

    大纲: (1) 入门例子: (2) 国际化: (3) 在代码中添加错误信息: (1) 入门例子: Validator主要是校验用户提交的数据的合理性的,比如是否为空了,密码长度是否大于6位,是否是纯数 ...

  3. (6)Spring Boot datasource - mysql【从零开始学Spring Boot】

    在任何一个平台都逃离不了数据库的操作,那么在spring boot中怎么接入数据库呢? 很简单,我们需要在application.properties进行配置一下,application.proper ...

  4. (3)Spring Boot热部署【从零开始学Spring Boot】

    在编写代码的时候,你会发现我们只是简单把打印信息改变了下,就需要重新部署,如果是这样的编码方式,那么我们估计一天下来之后就真的是打几个Hello World之后就下班了.那么如何解决热部署的问题呢?那 ...

  5. (42)Spring Boot多数据源【从零开始学Spring Boot】

    我们在开发过程中可能需要用到多个数据源,我们有一个项目(MySQL)就是和别的项目(SQL Server)混合使用了.其中SQL Server是别的公司开发的,有些基本数据需要从他们平台进行调取,那么 ...

  6. (27)Spring Boot Junit单元测试【从零开始学Spring Boot】

    Junit这种老技术,现在又拿出来说,不为别的,某种程度上来说,更是为了要说明它在项目中的重要性. 那么先简单说一下为什么要写测试用例 1. 可以避免测试点的遗漏,为了更好的进行测试,可以提高测试效率 ...

  7. 18. 使用模板【从零开始学Spring Boot】

    转:http://blog.csdn.net/linxingliang/article/details/52017098 18.1 使用thymeleaf 整体步骤: (1)       在pom.x ...

  8. 84. Spring Boot集成MongoDB【从零开始学Spring Boot】

    至于MongoDB网上有很多相关的资料,所以在这里不进行过多的介绍,我们在这里主要是介绍下如何将mongodb与spring boot结合使用.本节大纲: (1) 准备工作: (2) 新建一个mave ...

  9. Spring Boot配置ContextPath【从零开始学Spring Boot】

    Spring boot默认是/ ,这样直接通过http://ip:port/就可以访问到index页面,如果要修改为http://ip:port/path/ 访问的话,那么需要在Application ...

  10. 86. Spring Boot集成ActiveMQ【从零开始学Spring Boot】

    在Spring Boot中集成ActiveMQ相对还是比较简单的,都不需要安装什么服务,默认使用内存的activeMQ,当然配合ActiveMQ Server会更好.在这里我们简单介绍怎么使用,本节主 ...

随机推荐

  1. ios in-house 公布整个过程(startssl认证)

    首先大体说一下步骤: 1.申请苹果enterprise 账号 为应用生成app id,provision profile等 详见:http://www.th7.cn/Program/IOS/20131 ...

  2. nagios,zabbix对照

    nagios/zabbix对照: nagios核心功能是监控报警.是一个轻量化的监控系统. 假设须要图标显示,须要添加图标显示插件(如pnp4nagios): 假设须要存入数据库,须要对应的插件(ND ...

  3. 00084_Map接口

    1.Map接口概述 通过查看Map接口描述,发现Map接口下的集合与Collection接口下的集合,它们存储数据的形式不同. (1)Collection中的集合,元素是孤立存在的(理解为单身),向集 ...

  4. Onsctl 配置ONS服务(10G)

    Onsctl Onsctl这个命令是用来管理ONS(Oracle Notification Service)是OracleClustser实现FAN Event Push模型的基础. 在RAC环境下. ...

  5. Android开发经验之获取画在画布上的字符串长度、宽度(所占像素宽度)

    Android中获取字符串长度.宽度(所占像素宽度) 计算出当前绘制出来的字符串有多宽,可以这么来! 方法1: Paint paint = new Paint(); Rect rect = new R ...

  6. UDP 打洞示例 包含 服务器 客户端

    客户端示例: #include "Net.h" #include "../p2pInfo.h" int main() { CUdp  udp; if (0!=u ...

  7. SGU 253 Theodore Roosevelt 快速判断点是否在凸包内

    http://acm.sgu.ru/problem.php?contest=0&problem=253 题意简单易懂...给你n个点的凸包(经测试已经是极角序)...判断m个点是否在凸包内.. ...

  8. BZOJ3192: [JLOI2013]删除物品(splay)

    Description   箱子再分配问题需要解决如下问题:  (1)一共有N个物品,堆成M堆.  (2)所有物品都是一样的,但是它们有不同的优先级.  (3)你只能够移动某堆中位于顶端的物品.  ( ...

  9. Mac 终端操作数据库

    名词解释: 事务:一个事务(transaction)中的所有操作,要么全部完成,要么全部不完成,不会结束在中间某个环节.事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状态,就像这 ...

  10. Docker 内程序时间设置,很重要

    原文:Docker 内程序时间设置,很重要 重要!!!!! 创建容器时候需要修改一个参数,设置tomcat的时区 -e TZ="Asia/Shanghai" -v /etc/loc ...