FreeMarker 入门教程

欢迎来到 FreeMarker 入门教程

随便写的,有什么不足请留言指教

项目运行截图

项目url:http://localhost:8080/user
项目运行后浏览器的截图

git 下载地址

项目链接:https://github.com/oumingyuan/freemarker.git

软件环境

运行我的项目,你需要如下的环境

名称版本
java环境JDK 8
集成开发环境IDEA
打包工具gradle
版本工具git

项目重要文件的目录结构

重要的文件是用红色字体标注的

  • FreeMaker
    • src
      • main
        • java
          • com.example.freemarker
            • controller
              • UserController.java
            • entity
              • User.java
        • resource
          • templates
            • user
              • list.ftl
          • application.yml
      • build.gradle

UserController.java

package com.example.freemarker.controller;

import java.util.LinkedList;
import java.util.List;

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

@Controller
public class UserController {
    @RequestMapping("user")
    public String user(Model m) {
        List<User> list = new LinkedList<>();
        User u1 = new User(1, "hello1", "13336980260");
        User u2 = new User(2, "hello2", "13336980260");
        User u3 = new User(3, "hello3", "13336980260");
        list.add(u1);
        list.add(u2);
        list.add(u3);
        m.addAttribute("userList", list);
        m.addAttribute("listName", "user list");
        return "user/list";
    }
}

User.java

package com.example.freemarker.entity;


public class User {
    private long id;
    private String name;
    private String phone;

    public User(long id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }


    public String getPhone() {
        return phone;
    }


}

list.ftl

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" lang="">
<head>
    <meta content="text/html;charset=utf-8"/>
    <title>freemarker</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/popper.js/1.12.5/umd/popper.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/js/bootstrap.min.js"></script>


    <style>

        h2 {
            text-align: center;
            margin-top: 50px;
            margin-bottom: 30px;
        }
    </style>
</head>
<body>
<div class="container">

    <h2>${listName}</h2>
    <table class="table table-hover">

        <thead>
        <tr>
            <th>id</th>
            <th>name</th>
            <th>phone</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>1</td>
            <td>LiLei</td>
            <td>13336980260</td>
        </tr>
        <tr>
            <td>2</td>
            <td>HanMei</td>
            <td>15609682037</td>
        </tr>
        <#list userList as user>
            <tr>
                <td>${user.id}</td>
                <td>${user.name}</td>
                <td>${user.phone}</td>
            </tr>
        </#list>

        </tbody>
    </table>
</div>
</body>
</html>

application.yml

spring:
  freemarker:
    allow-request-override: false
    cache: false
    check-template-location: true
    charset: utf-8
    content-type: text/html
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: false
    suffix: .ftl
    template-loader-path: classpath:/templates/


build.gradle

plugins {
    id 'org.springframework.boot' version '2.1.4.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-freemarker'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值