freemarker 入门

新建工程 freemarker-demo

        1.  导入依赖

                <parent>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-parent</artifactId>
                        <version>2.2.8.RELEASE</version>
                    </parent>

                    <properties>
                        <java.version>1.8</java.version>
                    </properties>
                    <dependencies>
                        <dependency>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <optional>true</optional>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-web</artifactId>
                        </dependency>
                        <dependency>
                                 <groupId>org.springframework.boot</groupId>
                                    <artifactId>spring-boot-starter-freemarker</artifactId>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-test</artifactId>
                        </dependency><!-- apache 对 java io 的封装工具库 -->
                        <dependency>
                            <groupId>org.apache.commons</groupId>
                            <artifactId>commons-io</artifactId>
                            <version>1.3.2</version>
                        </dependency>
                    </dependencies>

        2. 启动类

        package com.freemarker;
​
        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
​
        @SpringBootApplication
        public class WebApp {
            public static void main(String[] args) {
                SpringApplication.run(WebApp.class, args);
            }
        }
        

        3. 配置文件

        server:
          port: 8001
        spring:
          application:
            name: freemarker-demo
          freemarker:
            suffix: .ftl

        4. 模板文件

                <!DOCTYPE html>
                <html>
                <head>
                    <meta charset="utf-8">
                    <title>01-basic</title>
                </head>
                <body>
                <b>普通文本 String 展示:</b><br><br>
                Hello ${name} <br>
                <hr>
                <b>对象Student中的数据展示:</b><br/>
                    姓名:${stu.name}<br/>
                    年龄:${stu.age}
                <hr>
                </body>
                </html>

        5. 实体类

        package com.freemarker.entity;
​
        import lombok.AllArgsConstructor;
        import lombok.Data;
        import lombok.NoArgsConstructor;
​
        @Data
        @NoArgsConstructor
        @AllArgsConstructor
        public class Student {
            private String name;
            private Integer age;
        }

        6. 控制器                

        package com.freemarker.controller;

        import com.heima.freemarker.entity.Student;
        import org.springframework.stereotype.Controller;
        import org.springframework.ui.Model;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.RequestMapping;

        @Controller
        @RequestMapping("/test")
        public class TestController {

            @GetMapping("/basic")
            public String basic(Model model) {
                // 1.纯文本形式的参数
                model.addAttribute("name", "zhangsan");
                // 2.实体类相关的参数
                Student student = new Student("lisi", 18);
                model.addAttribute("stu", student);
                return "01-basic";
            }
        }

******

freemarker 基础语法:

  --list

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>02-list</title>
</head>
<body>
<table>
<#list stus as stu>
<tr>
    <td>${stu_index+1}</td>
    <td>${stu.name}</td>
    <td>${stu.age}</td>
</tr></#list>
</table>
</body>
</html>
​
添加控制器方法
@GetMapping("/list")
public String list(Model model){
    List<Student> list = new ArrayList<>();
    list.add(new Student("zhangsan",18));
    list.add(new Student("lisi",20));
    list.add(new Student("wangwu",19));
    model.addAttribute("stus", list);
    return "02-list";
}

  --map

添加模板文件 03-map.ftl

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>03-map</title>
</head>
<body>
<ul>
<#list userMap?keys as key>
    <li>key:${key}--value:${userMap["${key}"]}</li>
</#list>
</ul>
</body>
</html>

添加控制器方法

@GetMapping("/map")
public String map(Model model){
    Map<String,Object> map = new HashMap<>();
    map.put("zhangsan",18);
    map.put("lisi",22);
    model.addAttribute("userMap", map);
    return "03-map";
}

  --条件判断

添加模板 04-if.ftl

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>02-list</title>
</head>
<body>
<table>
<#list stus as stu>
<#if stu.name='小红'>
    <tr style="color: red">
        <td>${stu_index+1}</td>
        <td>${stu.name}</td>
        <td>${stu.age}</td>
    </tr><#else >
    <tr>
        <td>${stu_index+1}</td>
        <td>${stu.name}</td>
        <td>${stu.age}</td>
    </tr></#if>
</#list>
</table>
</body>
</html>

添加方法

@GetMapping("/if")
public String condition(Model model){
    List<Student> list = new ArrayList<>();
    list.add(new Student("小红",18));
    list.add(new Student("小明",20));
    model.addAttribute("stus", list);
    return "04-if";
}

测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值