码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-01-18 15:06:26      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:eem   原创   配置   head   back   tag   mapping   normal   you   

视频&交流平台

à 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

Xml代码  技术分享图片
  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.    
  5.   <groupId>com.kfit</groupId>  
  6.   <artifactId>spring-boot-velocity</artifactId>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <packaging>jar</packaging>  
  9.    
  10.   <name>spring-boot-velocity</name>  
  11.   <url>http://maven.apache.org</url>  
  12.    
  13.   <properties>  
  14.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.      <!-- jdk版本号,angel在这里使用1.8,大家修改为大家本地配置的jdk版本号即可 -->  
  16.     <java.version>1.8</java.version>  
  17.   </properties>  
  18.    
  19.     <!--  
  20.        spring boot 父节点依赖,  
  21.        引入这个之后相关的引入就不需要添加version配置,  
  22.        spring boot会自动选择最合适的版本进行添加。  
  23.      -->  
  24.     <parent>  
  25.        <groupId>org.springframework.boot</groupId>  
  26.        <artifactId>spring-boot-starter-parent</artifactId>  
  27.        <version>1.4.1.RELEASE</version><!-- 1.4.1.RELEASE , 1.3.3.RELEASE-->  
  28.     </parent>  
  29.    
  30.   <dependencies>  
  31.     <dependency>  
  32.       <groupId>junit</groupId>  
  33.       <artifactId>junit</artifactId>  
  34.       <scope>test</scope>  
  35.     </dependency>  
  36.      
  37.         <!-- spring boot web支持:mvc,aop... -->  
  38.     <dependency>  
  39.        <groupId>org.springframework.boot</groupId>  
  40.        <artifactId>spring-boot-starter-web</artifactId>  
  41.     </dependency>  
  42.      
  43.     <!-- 引入freeMarker的依赖包. -->  
  44.     <dependency>     
  45.         <groupId>org.springframework.boot</groupId>    
  46.         <artifactId>spring-boot-starter-freemarker</artifactId>  
  47.     </dependency>  
  48.      
  49.   </dependencies>  
  50. </project>  

 

 

(4) 编写启动类;

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

Java代码  技术分享图片
  1. package com.kfit;  
  2.    
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5.    
  6. /** 
  7.  * 
  8.  * @author Angel --守护天使 
  9.  * @version v.0.1 
  10.  * @date 2016年10月4日 
  11.  */  
  12. @SpringBootApplication  
  13. public class App {  
  14.     publicstaticvoid main(String[] args) {  
  15.        SpringApplication.run(App.class, args);  
  16.     }  
  17. }  

 

 

(5) 编写模板文件hello.ftl;

       编写一个hello.ftl文件,此文件的路径在src/main/resources/templates下,其中hello.ftl文件的内容如下:

Html代码  技术分享图片
  1. <html>   
  2. <body>   
  3.     welcome ${name}  to freemarker!  
  4. </body>   
  5. </html>  

 

 

(6) 编写访问类HelloController;

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

Java代码  技术分享图片
  1. package com.kfit.demo.web;  
  2.    
  3. import java.util.Map;  
  4.    
  5. import org.springframework.stereotype.Controller;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7.    
  8. /** 
  9.  * 测试velocity; 
  10.  * @author Angel --守护天使 
  11.  * @version v.0.1 
  12.  * @date 2016年10月4日 
  13.  */  
  14. @Controller  
  15. public class HelloController {  
  16.      
  17.     @RequestMapping("/hello")  
  18.     public String hello(Map<String,Object> map){  
  19.        map.put("name""[Angel -- 守护天使]");  
  20.        return "hello";  
  21.     }  
  22.      
  23. }  

 

 

(7) 测试;

       好了,到这里,我们就可以启动我们的程序进行测试了,访问地址:

http://127.0.0.1:8080/hello ,如果你在浏览器中看到如下信息:

welcome [Angel -- 守护天使] to freemarker!

那么说明你的demo ok 了。

 

(8) freemarker配置;

       spring bootapplication.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

       首先我们改造下HelloControllerhello方法

Java代码  技术分享图片
  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.         
  6.        List<Map<String,Object>> friends =new ArrayList<Map<String,Object>>();  
  7.        Map<String,Object> friend = new HashMap<String,Object>();  
  8.        friend.put("name""张三");  
  9.        friend.put("age"20);  
  10.        friends.add(friend);  
  11.        friend = new HashMap<String,Object>();  
  12.        friend.put("name""李四");  
  13.        friend.put("age"22);  
  14.        friends.add(friend);  
  15.        map.put("friends", friends);  
  16.        return "hello";  
  17.     }  

 

       这里我们返回了genderfriends的列表;

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

Html代码  技术分享图片
  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.         
  12.         
  13.        <p>性别:  
  14.            <#if gender==0>  
  15.               女  
  16.            <#elseif gender==1>  
  17.               男  
  18.            <#else>  
  19.               保密     
  20.            </#if>  
  21.         </p>  
  22.         
  23.         
  24.        <h4>我的好友:</h4>  
  25.        <#list friends as item>  
  26.            姓名:${item.name} , 年龄${item.age}  
  27.            <br>  
  28.        </#list>  
  29.         
  30.     </body>  
  31. </html>  

 

 

 

(10) freemarker layout

       freemarker layout主要处理具有相同内容的页面,比如每个网站的headerfooter页面。

       freemarker 的布局主要常见的两种方式是#import(“文件路径”)#include(“文件路径”),其中importinclude的区别在于,include常用于公共部分的页面,如果要使用<#assign username=“张三”>涉及到内部函数以及变量声明之类的,使用import进行导入,如果在import中的页面含有页面当前将不会进行渲染。   我们编写一个headerfooter,其中的header使用include引入,footer页面也使用include引入。(当然freemarker 还有别的布局方式,这里只是介绍一种,请自行学习研究)

       header.ftl内容:

Html代码  技术分享图片
  1. <header>  
  2.     This is a header,welcome  ${name} to my web site!  
  3. </header>  
  4. <hr>  

 

   

 

       footer.ftl内容:

Html代码  技术分享图片
  1. <hr>  
  2. <footer>  
  3.     This is a footer,welcome  ${name} to my web site!  
  4. </footer>  

 

       修改hello.ftl

Html代码  技术分享图片
  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.      
  9.         <#include "/header.ftl" >  
  10.          
  11.        <p>  
  12.            welcome ${name}  to freemarker!  
  13.        </p>        
  14.         
  15.         
  16.        <p>性别:  
  17.            <#if gender==0>  
  18.               女  
  19.            <#elseif gender==1>  
  20.               男  
  21.            <#else>  
  22.               保密     
  23.            </#if>  
  24.         </p>  
  25.         
  26.         
  27.        <h4>我的好友:</h4>  
  28.        <#list friends as item>  
  29.            姓名:${item.name} , 年龄${item.age}  
  30.            <br>  
  31.        </#list>  
  32.         
  33.         
  34.        <#include "/footer.ftl" >  
  35.     </body>  
  36. </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(转)

标签:eem   原创   配置   head   back   tag   mapping   normal   you   

原文地址:https://www.cnblogs.com/jpfss/p/8309996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!