SpringBoot集成Freemarker

本文介绍了如何在SpringBoot项目中集成Freemarker,包括添加依赖、配置设置、创建templates目录存放模板文件(.ftl),以及如何使用模板语法如$、<#if>、<#list>来展示和处理后台传来的数据。通过示例展示了在控制器中传递数据到模板并进行条件和列表渲染的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具。

pom文件添加依赖

  <!-- freemarker -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

springboot配置文件添加freemaker配置:
注意resources文件夹下新建templates文件夹作文模板的存放目录类似jsp,模板文件后缀:ftl

#设定ftl文件路径
spring.freemarker.template-loader-path=classpath:templates
#关闭缓存即时刷新
spring.freemarker.cache=false
#设定template的编码
spring.freemarker.charset=UTF-8
#是否检查templete路径是否存在
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html; charset=utf-8

#设定所有的request的属性merge到末班的时候是否都要添加到model中
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
#制定requestContext属性的名
spring.freemarker.request-context-attribute=request
#设定模板后缀
spring.freemarker.suffix=.ftl

使用模板获取数据并展示:
$用于获取后台传来参数对象,controller中可以放在model中也可以放在map中
<#if> <#else> 类似java中的if else
<#list > 用户展示list数据集合 userList as res 其中uesrList 是后台传输来的数据集合

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
</head>
	<body>

	${user.name}
	<#if name=="smf">
			上海
		<#elseif name=="25">
		smf
		<#else>
		广州
	</#if>
	
	<#list userList as res>
	${res.name}
	</#list>
	
	<h1>hello</h1>
	</body>
</html>

后台 controller代码,本项目基于上一篇博客的项目继续开发,所以关于mybatis的相关内容可以参考:https://blog.youkuaiyun.com/Phoenix_smf/article/details/107492625

package com.example.demo.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.model.User;
import com.example.demo.service.UserService;

@RequestMapping("/user")
@Controller
public class UserController {
    @Autowired
	private UserService userService;
	
    /**根据id查找用户信息*/
    @RequestMapping("/getUser")
    @ResponseBody  
	public User getUserById(int id) {
		return  userService.selectByPrimaryKey(id);
	}
    
    
    /**freemaker数据展示*/
    @RequestMapping("/freemakerPage")
    public String freemakerPage(Model model) {
    	User user = userService.selectByPrimaryKey(1);
    	model.addAttribute("user", user);
    	model.addAttribute("name", "smf");
    	model.addAttribute("age", 25);    	
    	User user2 = new User();
    	user2.setName("mmy");   	
    	List<User> userList  = new ArrayList<User>();
    	userList.add(user);
    	userList.add(user2);
    	model.addAttribute("userList", userList);    	
		return "test";  	
    }
}

浏览器输入访问地址:
在这里插入图片描述

Spring Boot可以集成Freemarker作为视图格式,以便在Spring MVC中使用。为了创建一个Spring Boot Freemarker工程并测试模板,你需要执行以下步骤: 1. 创建一个测试工程,并在pom.xml文件中添加必要的依赖项。其中包括以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> ``` 2. 创建一个Controller类,在这个类中将需要的数据添加到Map中,并返回模板文件的名称。例如: ``` @Controller public class HelloController { @GetMapping("/basic") public String test(Model model) { // 纯文本形式的参数 model.addAttribute("name", "freemarker"); // 实体类相关的参数 Student student = new Student(); student.setName("小明"); student.setAge(18); model.addAttribute("stu", student); return "01-basic"; } } ``` 3. 创建一个启动类,使用@SpringBootApplication注解标记,并在main方法中运行Spring Boot应用程序。例如: ``` @SpringBootApplication public class FreemarkerDemotApplication { public static void main(String[] args) { SpringApplication.run(FreemarkerDemotApplication.class, args); } } ``` 4. 运行测试,访问对应的URL来查看Freemarker模板渲染的结果。 以上是使用Spring Boot集成Freemarker的基本步骤。你可以根据具体的需求和业务逻辑进行相应的调整和配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Springboot集成Freemarker|超级详细,建议收藏](https://blog.csdn.net/heima005/article/details/129435750)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Springboot整合FreeMarker](https://blog.csdn.net/zxy15974062965/article/details/122818338)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Phoenix_smf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值