最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年10月04日 10:05:13   作者:林祥纖  
本篇文章主要介紹了Spring Boot使用模板freemarker的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

最近有好久沒有更新博客了,感謝小伙伴的默默支持,不知道是誰又打賞了我一個小紅包,謝謝。

今天我們講講怎么在Spring Boot中使用模板引擎freemarker,先看看今天的大綱:

(1) freemarker介紹;
(2) 新建spring-boot-freemarker工程;
(3) 在pom.xml引入相關(guān)依賴;
(4) 編寫啟動類;
(5) 編寫模板文件hello.ftl;
(6) 編寫訪問類HelloController;
(7) 測試;
(8) freemarker配置;
(9) freemarker常用語法;
(10) freemarker layout 布局

(1) freemarker介紹;

FreeMarker是一款模板引擎: 即一種基于模板和要改變的數(shù)據(jù),   并用來生成輸出文本(HTML網(wǎng)頁、電子郵件、配置文件、源代碼等)的通用工具。       它不是面向最終用戶的,而是一個Java類庫,是一款程序員可以嵌入他們所開發(fā)產(chǎn)品的組件。

(2) 新建spring-boot-freeMarker工程;

我們新建一個maven工程,取名為:spring-boot-freemarker

(3) 在pom.xml引入相關(guān)依賴;

這里使用freeMarker需要引入相關(guān)依賴包: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 父節(jié)點依賴, 
    引入這個之后相關(guān)的引入就不需要添加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文件的內(nèi)容如下:

<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的語法并不是本節(jié)的重點,這里還是簡單的介紹下幾個常用的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主要處理具有相同內(nèi)容的頁面,比如每個網(wǎng)站的header和footer頁面。

freemarker 的布局主要常見的兩種方式是#import(“文件路徑”)和#include(“文件路徑”),其中import和include的區(qū)別在于,include常用于公共部分的頁面,如果要使用<#assign username=“張三”>涉及到內(nèi)部函數(shù)以及變量聲明之類的,使用import進行導(dǎo)入,如果在import中的頁面含有頁面當(dāng)前將不會進行渲染。   我們編寫一個header和footer,其中的header使用include引入,footer頁面也使用include引入。(當(dāng)然freemarker 還有別的布局方式,這里只是介紹一種,請自行學(xué)習(xí)研究)

header.ftl內(nèi)容:

<header> 
  This is a header,welcome ${name} to my web site! 
</header> 
<hr> 

footer.ftl內(nèi)容:

<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頁面,應(yīng)該會看到如下圖的效果:

 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java多線程面試題(面試官常問)

    Java多線程面試題(面試官常問)

    這篇文章主要介紹了Java多線程面試題(面試官常問),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • idea中引入了gb2312編碼的文件的解決方法

    idea中引入了gb2312編碼的文件的解決方法

    這篇文章主要介紹了idea中引入了gb2312編碼的文件的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Java?-jar參數(shù)設(shè)置小結(jié)

    Java?-jar參數(shù)設(shè)置小結(jié)

    本文主要介紹了Java?-jar參數(shù)設(shè)置小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 使用Java將字符串在ISO-8859-1和UTF-8之間相互轉(zhuǎn)換

    使用Java將字符串在ISO-8859-1和UTF-8之間相互轉(zhuǎn)換

    大家都知道在一些情況下,我們需要特殊的編碼格式,如:UTF-8,但是系統(tǒng)默認的編碼為ISO-8859-1,遇到這個問題,該如何對字符串進行兩個編碼的轉(zhuǎn)換呢,下面小編給大家分享下java中如何在ISO-8859-1和UTF-8之間相互轉(zhuǎn)換,感興趣的朋友一起看看吧
    2021-12-12
  • Idea去除方法形參參數(shù)提示的操作

    Idea去除方法形參參數(shù)提示的操作

    這篇文章主要介紹了Idea去除方法形參參數(shù)提示的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • springboot日志沒有記錄異常問題及解決

    springboot日志沒有記錄異常問題及解決

    在Spring Boot項目中,定時任務(wù)在服務(wù)器上運行時報錯但未記錄日志,本地運行時控制臺能打印報錯信息,但日志中無記錄,問題出在報錯發(fā)生在線程池中,通過繼承ThreadPoolExecutor并重寫afterExecute方法,可以將異常信息記錄到日志中
    2025-02-02
  • java通過PDF模板填寫PDF表單

    java通過PDF模板填寫PDF表單

    這篇文章主要為大家詳細介紹了java通過PDF模板填寫PDF表單,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • 兩個List集合取相同重復(fù)數(shù)據(jù)的方法

    兩個List集合取相同重復(fù)數(shù)據(jù)的方法

    今天小編就為大家分享一篇關(guān)于兩個List集合取相同重復(fù)數(shù)據(jù)的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • SpringCloud服務(wù)網(wǎng)關(guān)Gateway的使用教程詳解

    SpringCloud服務(wù)網(wǎng)關(guān)Gateway的使用教程詳解

    SpringCloud Gateway是Spring體系內(nèi)的一個全新項目,它旨在為微服務(wù)架構(gòu)提供一種簡單有效的統(tǒng)一的API路由管理方式。本文就來為大家詳細講講Gateway的使用教程,需要的可以參考一下
    2022-09-09
  • Java ResultSet案例講解

    Java ResultSet案例講解

    這篇文章主要介紹了Java ResultSet案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08

最新評論

额尔古纳市| 微山县| 灌云县| 屏东县| 马边| 金华市| 密云县| 连州市| 合川市| 营山县| 峨边| 宽甸| 上犹县| 青川县| 抚松县| 枞阳县| 崇仁县| 永城市| 桑植县| 锡林浩特市| 延长县| 淮阳县| 平原县| 邹城市| 泽州县| 进贤县| 融水| 济南市| 玛纳斯县| 安陆市| 东海县| 北碚区| 房山区| 莱芜市| 武川县| 贵阳市| 全椒县| 黄大仙区| 宜良县| 阳春市| 阜新|