spring boot 中freemaker的全局变量如何配置
时间: 2023-09-02 21:12:04 浏览: 61
您好,关于Spring Boot中Freemaker的全局变量配置方法,您可以在application.properties文件中添加以下内容:
```
spring.freemarker.settings.variable=globalName=globalValue
```
其中"globalName"为全局变量名称,"globalValue"为全局变量值。若需要配置多个全局变量,在"variable"后添加逗号再配置即可。希望我的回答对您有所帮助。
相关问题
spring boot整合 freemarker 详细教程
Spring Boot整合Freemarker的教程如下:
1.在Maven中添加Freemarker依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
```
2.在application.properties中配置Freemarker相关属性:
```properties
# Freemarker设置
# 模板路径
spring.freemarker.template-loader-path=classpath:/templates/
# 字符编码
spring.freemarker.charset=UTF-8
# 后缀名
spring.freemarker.suffix=.ftl
# 是否开启缓存
spring.freemarker.cache=false
# 内容类型
spring.freemarker.content-type=text/html
```
3.创建Freemarker模板,例如index.ftl:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Freemarker示例</title>
</head>
<body>
<h1>Hello ${name}!</h1>
</body>
</html>
```
4.创建Controller,例如IndexController:
```java
@Controller
public class IndexController {
@GetMapping("/")
public String index(Model model) {
model.addAttribute("name", "Freemarker");
return "index";
}
}
```
5.运行Spring Boot应用程序,并访问http://localhost:8080/,即可看到渲染后的页面。
以上就是Spring Boot整合Freemarker的详细教程,希望对你有帮助!
Spring boot集成freemarker导出excel
Spring Boot集成Freemarker导出Excel可以通过以下几个步骤来实现:
1. 添加依赖
在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
```
2. 创建Freemarker模板
创建一个freemarker模板,用于指定导出的excel文件的格式和样式。例如,可以创建一个名为`template.ftl`的模板文件,其中包含以下内容:
```html
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>地址</th>
</tr>
</thead>
<tbody>
<#list users as user>
<tr>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.gender}</td>
<td>${user.address}</td>
</tr>
</#list>
</tbody>
</table>
```
3. 创建Controller
创建一个Controller,用于接收请求并生成excel文件。例如,可以创建一个名为`ExportController`的Controller,其中包含以下代码:
```java
@Controller
public class ExportController {
@Autowired
private Configuration freemarkerConfig;
@RequestMapping("/export")
public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 设置响应头
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=users.xls");
// 获取模板
Template template = freemarkerConfig.getTemplate("template.ftl");
// 创建数据模型
Map<String, Object> model = new HashMap<String, Object>();
List<User> users = getUserList();
model.put("users", users);
// 生成excel文件
Writer out = response.getWriter();
template.process(model, out);
out.flush();
out.close();
}
private List<User> getUserList() {
List<User> users = new ArrayList<User>();
// 添加测试数据
users.add(new User("张三", 25, "男", "北京市海淀区"));
users.add(new User("李四", 30, "女", "上海市浦东新区"));
users.add(new User("王五", 35, "男", "广州市天河区"));
users.add(new User("赵六", 40, "女", "深圳市福田区"));
return users;
}
}
```
4. 运行程序
启动Spring Boot应用程序,并访问`/export`路径即可下载生成的excel文件。
以上就是通过Spring Boot集成Freemarker导出Excel的步骤。需要注意的是,本例中使用的是xls格式的excel文件,如果需要使用xlsx格式,需要将poi-ooxml依赖修改为poi-ooxml-schemas。