• SpringBoot: 8.整合freemarker(转)


     

    1、创建maven项目,添加pom依赖

    复制代码
    <!--springboot项目依赖的父项目-->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
        </parent>
    
        <dependencies>
            <!--注入springboot启动器-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!--注入springboot对freemarker视图技术的支持-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>
        </dependencies>
    复制代码

    2、创建controller

    复制代码
    package com.bjsxt.controller;
    
    import com.bjsxt.pojo.User;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Created by Administrator on 2019/2/6.
     */
    @Controller
    public class UserController {
    
        @RequestMapping("/toUserList")
        public String toUserList(Model model){
            List<User> userList=new ArrayList<User>();
            userList.add(new User(1L,"张三","男"));
            userList.add(new User(2L,"李四","女"));
            userList.add(new User(3L,"王五","男"));
            model.addAttribute("userList",userList);
            return "user_list";
        }
    }
    复制代码

    3、创建freemarker模版文件user_list.ftl

    注意:springboot 要求模板形式的视图层技术的文件必须要放到 src/main/resources 目录下必
    须要一个名称为 templates

    复制代码
    <html>
    <head>
        <title>用户列表</title>
    </head>
    <body>
    
    <table border="1px solid red">
        <tr>
            <th>id</th>
            <th>姓名</th>
            <th>性别</th>
        </tr>
        <#list userList as user>
            <tr>
                <td>${user.id}</td>
                <td>${user.name}</td>
                <td>${user.sex}</td>
            </tr>
        </#list>
    </table>
    </body>
    </html>
    复制代码

    4、创建启动器

    复制代码
    package com.bjsxt;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * Created by Administrator on 2019/2/6.
     */
    @SpringBootApplication
    public class App {
    
        public static void main(String[] args){
            SpringApplication.run(App.class,args);
        }
    }
    复制代码

     目录结构

  • 相关阅读:
    Squid报错:error the requested url could not be retriveved
    理解交换机的工作原理
    1_bytes和str
    2_Linux操作系统和基础命令行
    1_Linux概述
    好用的手机浏览器
    笔记(一):做前端开发以来几乎每天用到的东西!
    笔记(一):做前端开发以来几乎每天用到的东西!
    积累: .net里有个线程安全的int+1类
    积累: .net里有个线程安全的int+1类
  • 原文地址:https://www.cnblogs.com/kuangzhisen/p/10427159.html
Copyright © 2020-2023  润新知