关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

Spring Boot整合静态化技术FreeMarker

发布时间:2020-03-24 00:00:00

原文链接:http://www.yiidian.com/springboot/springboot-freemarker.html

本文讲解如何在Spring Boot整合FreeMarker。

1 创建项目,导入依赖

4.0.0com.yiidianch03_05_springboot_freemarker1.0-SNAPSHOTorg.springframework.bootspring-boot-starter-parent2.1.11.RELEASEorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-freemarker

这里记得要到FreeMarker的依赖,否则无法运行!

2 编写Controller

package com.yiidian.controller;
import java.util.ArrayList;
import java.util.List;

import com.yiidian.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 控制器
 * 一点教程网 - www.yiidian.com
 */
@Controller
public class UserController {

	/**
	 * 用户列表展示
	 */
	@RequestMapping("/list")
	public String list(Model model){
		//模拟用户数据
		Listlist = new ArrayList();
		list.add(new User(1,"小张",18));
		list.add(new User(2,"小李",20));
		list.add(new User(3,"小陈",22));

		//把数据存入model
		model.addAttribute("list", list);

		//跳转到freemarker页面: list.ftl
		return "list";
	}
}

在Controller把数据传递到FreeMarker模板渲染

3 编写FreeMarker模板文件

file

FreeMarker模板文件必须放在/resources/templates目录下,后缀名为.ftl,内容如下:

一点教程网-用户列表展示用户列表展示编号姓名年龄${user.id}${user.name}${user.age}

4 运行测试

http://localhost:8080/list

file

源码下载:https://pan.baidu.com/s/10WaOAfrWHG-v2M8QFe2zMA


/template/Home/Zkeys/PC/Static