springboot整合freemarker

http://www.cnblogs.com/nannan0226/p/6400273.html

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

前提:

开发工具:idea

框架:spring boot、maven

1、pom文件添加依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
            <version>1.4.1.RELEASE</version>
        </dependency>

2、新建spring web项目,会自动生成application.properties.

使用application.properties配置文件之后,spring boot启动时,会自动把配置信息读取到spring容器中,并覆盖spring boot的默认配置。

3、在配置文件中,设置以下两个参数

#设定ftl文件路径
spring.freemarker.template-loader-path=classpath:/templates
#设定静态文件路径,js,css等
spring.mvc.static-path-pattern=/static/**

controller跳转页面如下:
templates/action/list.ftl页面
 

 

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

 

(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>  

       footer.ftl内容:

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


  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Freemarker是一种模板引擎,可以将数据和模板进行整合生成输出内容。SpringBoot提供了对Freemarker的支持,可以很方便地整合Freemarker。 1. 添加依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> ``` 2. 配置文件 在application.properties文件中添加以下配置: ```properties spring.freemarker.template-loader-path=classpath:/templates/ spring.freemarker.cache=false ``` - template-loader-path:模板文件的路径,这里设置为classpath:/templates/,表示在项目的classpath下的templates目录中查找模板文件。 - cache:是否开启模板缓存,这里设置为false,表示关闭缓存。 3. 创建模板文件 在classpath:/templates/目录下创建一个名为index.ftl的模板文件,内容如下: ```html <!DOCTYPE html> <html> <head> <title>SpringBoot整合Freemarker</title> </head> <body> <h1>${message}</h1> </body> </html> ``` 4. 创建控制器 创建一个名为IndexController的控制器,代码如下: ```java @Controller public class IndexController { @RequestMapping("/") public String index(Model model) { model.addAttribute("message", "Hello, World!"); return "index"; } } ``` 该控制器中,使用@RequestMapping注解指定了请求路径为/,并将一个名为message的属性值设置为“Hello, World!”,然后返回了index作为视图名称。由于配置了spring.freemarker.template-loader-path=classpath:/templates/,所以SpringBoot会在classpath:/templates/目录下查找名为index的模板文件,并将模板文件中的${message}替换为“Hello, World!”。 5. 运行程序 启动应用程序,访问http://localhost:8080/,可以看到页面中显示了“Hello, World!”的字样。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值