SpringBoot学习——springboot整合FreeMarker框架

版权声明:转载请注明来源 https://blog.csdn.net/qq_24598601/article/details/89187795

一、pom 文件引入FreeMarker

  在原 SpringBoot 项目中的 POM 文件中加入 FreeMarker 坐标,如果不知道坐标可以从 Maven 中央库查询 http://mvnrepository.com/。当然,如果项目没有使用 Maven ,那就需要导入 FreeMarker Jar ,点击下载JAR ,此外还需要有 spring-context-support.jar

<!-- 引入freeMarker的依赖包. -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

二、Controller

package com.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bean.User;

/**
* @Description 测试整合freemarker
* @author 欧阳
* @since 2019年4月10日 下午3:55:10
* @version V1.0
*/
@Controller
public class IndexMarkerController {
	
	@RequestMapping("/indexmarker")
	public String indexmarker(Map<String, Object> map) {
		//添加标题
		map.put("title", "SpringBoot整合Freemarker");
		//添加人员
		List<User> result = new ArrayList<User>();
		result.add(new User("1", "zhangsan"));
		result.add(new User("2", "lisi"));
		result.add(new User("3", "王五"));
		map.put("users", result);
		return "indexmarker";
	}
}

  注意:使用注解 @Controller 而不要使用 @RestController ,否则会将返回的视图名解析成字符串返回到页面。如果使用 @RestController 注解,则需要返回 ModelAndView

三、application.properties 配置

  在 application.properties 文件中添加下面配置:

###Freemarker Configuration
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.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates/

  上面的配置使用 application.yml 文件配置如下:

###FREEMARKER Configuration
spring:
  freemarker:
    allow-request-override: false
    cache: true
    check-template-location: true
    charset: UTF-8
    content-type: text/html; charset=utf-8
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: false
    suffix: .ftl
    template-loader-path: 
    - classpath:/templates/
 

四、FTL 文件

  当使用 FreeMarker 模板引擎时,默认的模板配置路径为:src/main/resources/templates ,也可将默认配置路径修改为其他路径,方法是通过修改 application.properties 文件中的 spring.freemarker.template-loader-path 属性值。下面是模版 indexmarker.ftl 的主要代码。关于 FreeMarker 的语法自行百度。

<body>
<#if title == "1">
     <caption>标题</caption>
   <#elseif title == "2">
     <caption>测试</caption>
   <#else>
     <caption>${title}</caption>
</#if>
  <table class="table table-bordered table-hover">
  	<thead>
  		<tr>
  			<td>序号</td>
  			<td>姓名</td>
  		</tr>
  	</thead>
  	<tbody>
  		<#list users as user>
  		<tr>
  			<td>${user.id}</td>
  			<td>${user.name}</td>
  		</tr>
  		</#list>
  	</tbody>
  </table>
</body>

五、整合后结果

  浏览器访问地址:http://localhost:8080/indexmarker,后效果图如下:
springboot整合FreeMarker框架

猜你喜欢

转载自blog.csdn.net/qq_24598601/article/details/89187795