全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

Spring Boot使用模板freemarker的示例代码

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

今天我们讲讲怎么在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,

<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页面,应该会看到如下图的效果:

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# Spring  # Boot  # freemarker  # Boot使用freemarker  # Boot使用freemarker模板  # Springboot整合freemarker 404问题解决方案  # 基于Freemarker和xml实现Java导出word  # SpringBoot2.2.X用Freemarker出现404的解决  # SpringBoot使用FreeMarker模板发送邮件  # SpringBoot整合freemarker的讲解  # spring boot 集成 shiro 自定义密码验证 自定义freemarker标签根据权限渲染  # spring boot里增加表单验证hibernate-validator并在freemarker模  # 详解MyEclipse中搭建spring-boot+mybatis+freemarker框架  # 新手入门学习Spring Freemarker教程解析  # 是一款  # 几个  # 在这里  # 好了  # 有个  # 就不  # 之处  # 你在  # 两种  # 没有什么  # 也很  # 涉及到  # 将不  # 要使  # 配置文件  # 最合适  # 源代码  # 大家多多  # 好久没有  # 中为 


相关文章: 香港服务器部署网站为何提示未备案?  内网网站制作软件,内网的网站如何发布到外网?  深圳 网站制作,深圳招聘网站哪个比较好一点啊?  宁波免费建站如何选择可靠模板与平台?  如何自定义建站之星模板颜色并下载新样式?  已有域名能否直接搭建网站?  怎么用手机制作网站链接,dw怎么把手机适应页面变成网页?  建站之星如何开启自定义404页面避免用户流失?  如何快速启动建站代理加盟业务?  香港网站服务器数量如何影响SEO优化效果?  制作网站的基本流程,设计网站的软件是什么?  如何用低价快速搭建高质量网站?  我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?  活动邀请函制作网站有哪些,活动邀请函文案?  如何在西部数码注册域名并快速搭建网站?  唐山网站制作公司有哪些,唐山找工作哪个网站最靠谱?  如何高效配置IIS服务器搭建网站?  建站之星微信建站一键生成小程序+多端营销系统  建站之星代理商如何保障技术支持与售后服务?  惠州网站建设制作推广,惠州市华视达文化传媒有限公司怎么样?  微信网站制作公司有哪些,民生银行办理公司开户怎么在微信网页上查询进度?  简单实现Android验证码  官网网站制作腾讯审核要多久,联想路由器newifi官网  C++如何将C风格字符串(char*)转换为std::string?(代码示例)  如何破解联通资金短缺导致的基站建设难题?  如何自己制作一个网站链接,如何制作一个企业网站,建设网站的基本步骤有哪些?  宝塔建站后网页无法访问如何解决?  建站主机是否等同于虚拟主机?  家具网站制作软件,家具厂怎么跑业务?  如何快速上传建站程序避免常见错误?  C#如何序列化对象为XML XmlSerializer用法  如何通过.red域名打造高辨识度品牌网站?  宝塔建站无法访问?如何排查配置与端口问题?  c# Task.ConfigureAwait(true) 在什么场景下是必须的  韩国服务器如何优化跨境访问实现高效连接?  如何快速生成专业多端适配建站电话?  如何高效完成独享虚拟主机建站?  黑客入侵网站服务器的常见手法有哪些?  如何在橙子建站中快速调整背景颜色?  如何选择高效可靠的多用户建站源码资源?  网页设计与网站制作内容,怎样注册网站?  建站之星IIS配置教程:代码生成技巧与站点搭建指南  如何在阿里云虚拟机上搭建网站?步骤解析与避坑指南  零服务器AI建站解决方案:快速部署与云端平台低成本实践  建站主机解析:虚拟主机配置与服务器选择指南  儿童网站界面设计图片,中国少年儿童教育网站-怎么去注册?  建站之星免费版是否永久可用?  动图在线制作网站有哪些,滑动动图图集怎么做?  网站建设制作、微信公众号,公明人民医院怎么在网上预约?  如何在景安云服务器上绑定域名并配置虚拟主机? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。