Spring Boot整合Freemarker

一.首先导入依赖
<!-- 添加freemarker模版的依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
 
二.properties相关配置
## Freemarker 配置
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.request-context-attribute=request
 
 
spring.freemarker.prefix=/
spring.freemarker.suffix=.ftl
 
三.controller
package com.chx.springboot.controller;
 
 
import com.chx.springboot.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
 
 
import java.util.ArrayList;
import java.util.List;
 
 
@Controller
@RequestMapping("/free")
public class FreeController {
 
 
    @RequestMapping("/first")
    public String freeFirst(ModelMap map){
        map.put("name","chxinz");
 
 
        // 普通值
        List<String> list=new ArrayList<>();
        list.add("张三");
        list.add("张三三");
        list.add("张三三三");
        map.put("list",list);
 
 
        //用户泛型
        List<User> userList=new ArrayList<User>();
        User user = new User(1,"chx");
        userList.add(user);
        map.put("userList",userList);
        return "index";
    }
 
}
 
四.freemarker页面(.ftl)
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
欢迎:${name}
<#list list as user>
    ${user}
</#list>
<br/>
<#list userList as user>
    ${user.userName}
</#list>
</body>
</html>
 
五.执行结果
 

猜你喜欢

转载自www.cnblogs.com/chx9832/p/12017619.html