SpringBoot--整合FreeMarker--使用/实例

原文网址:SpringBoot--整合FreeMarker--使用/实例_IT利刃出鞘的博客-CSDN博客

简介

本文介绍SpringBoot如何使用FreeMarker。

配置文件

application.yml

spring:
  #模板引擎 freemarker
  freemarker:
    # 模板后缀
    suffix: .ftl
    # 是否启用模板缓存
    cache: false
    # 模板编码
    charset: UTF-8
    # 设置ftl文件根路径
    template-loader-path: classpath:/templates
    # 是否检查模板位置是否存在
    check-template-location: true
    # Content-Type value
    content-type: text/html
    # 设定所有request的属性在merge到模板的时候,是否要都添加到model中
    expose-request-attributes: true
    # 是否在merge模板的时候,将HttpSession属性都添加到model中
    expose-session-attributes: true
    # request-context为request
    request-context-attribute: request

依赖

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

后端代码

Controller

package com.example.knife.entity;

import lombok.Data;

@Data
public class User {
    private Integer age;

    private String name;

    private String email;
}

Entity

package com.example.knife.controller;

import com.example.knife.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class UserController {
    @GetMapping("user")
    public String user(Model model) {
        User user = new User();
        user.setAge(25);
        user.setEmail("[email protected]");
        user.setName("Tony");
        model.addAttribute("userInfo", user);
        return "user";
    }
}

前端代码

user.ftl(resources/templates目录下)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>FreeMarker 实验</title>
    <style>
        table,td {
            border: black 1px solid;
        }
    </style>
</head>
<body>
<table>
    <tr>
        <td>姓名</td>
        <td>年龄</td>
        <td>Email</td>
    </tr>
    <tr>
        <td>${userInfo.name}</td>
        <td>${userInfo.age}</td>
        <td>${userInfo.email}</td>
    </tr>
</table>
</body>
</html>

测试

访问:http://localhost:8080/user

测试List

修改Controller

package com.example.knife.controller;

import com.example.knife.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

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

@Controller
public class UserController {
    @GetMapping("users")
    public String users(Model model) {
        List<User> userList = new ArrayList<>();

        User user1 = new User();
        user1.setAge(25);
        user1.setEmail("[email protected]");
        user1.setName("Tony");

        User user2 = new User();
        user2.setAge(26);
        user2.setEmail("[email protected]");
        user2.setName("Peter");

        userList.add(user1);
        userList.add(user2);

        model.addAttribute("userInfos", userList);
        return "users";
    }
}

添加users.ftl(resources/templates目录)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>FreeMarker 实验</title>
    <style>
        table,td {
            border: black 1px solid;
        }
    </style>
</head>
<body>
<table>
    <tr>
        <td>姓名</td>
        <td>年龄</td>
        <td>Email</td>
    </tr>
    <#list userInfos as userInfo>
        <tr>
            <td>${userInfo.name}</td>
            <td>${userInfo.age}</td>
            <td>${userInfo.email}</td>
        </tr>
    </#list>

</table>
</body>
</html>

测试

访问:http://localhost:8080/users

猜你喜欢

转载自blog.csdn.net/feiying0canglang/article/details/128123230
今日推荐