• 十二.spring-boot使用spring-boot-freemarker


    ①、在springMVC中:它代表着view层组件

    ②、为什么使用freemarker:简单容易学、逻辑分明

    ③、freemarker优点:它不依赖servlet、网络或者web环境

    image

    一、创建一个maven web project

    imageimage

    二、检查freemarker包是否已经添加

    image

    三、编写测试类

    1、新建一个用户数据模型SysUser

    package com.example.entity;
    
    public class SysUser {
        private long id;
        private String name;
        private String phone;
        
        
        public SysUser() {
        }
        public SysUser(long id, String name, String phone) {
            this.id = id;
            this.name = name;
            this.phone = phone;
        }
        public long getId() {
            return id;
        }
        public void setId(long id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPhone() {
            return phone;
        }
        public void setPhone(String phone) {
            this.phone = phone;
        }
        @Override
        public String toString() {
            return "SysUser [id=" + id + ", name=" + name + ", phone=" + phone + "]";
        }
        
        
    }

    2、编写controller类

    package com.example.controller;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.example.entity.SysUser;
    
    @Controller
    public class Hello {
        
        @RequestMapping("hello")
        public String hello(Model m){
            m.addAttribute("name", "spring-boot");
            return "hello";
        }
        @RequestMapping("sysUser")
        public String user(Model m){
            List<SysUser> list = new ArrayList<SysUser>();
            SysUser u1 = new SysUser(0001, "hello1", "11111111111111111");
            SysUser u2 = new SysUser(0002, "hello2", "22222222222222222");
            SysUser u3 = new SysUser(0003, "hello3", "33333333333333333");
            list.add(u1);
            list.add(u2);
            list.add(u3);
            m.addAttribute("userList", list);
            m.addAttribute("sysUser", "SysUser");
            return "sysUser/list";
        }
    }

    3、配置application.properties文件

    ########################################################
    ###FREEMARKER (FreeMarkerAutoConfiguration)
    ########################################################
    spring.freemarker.allow-request-override=false
    spring.freemarker.cache=true
    spring.freemarker.check-template-location=true
    spring.freemarker.charset=UTF-8
    spring.freemarker.content-type=text/html
    spring.freemarker.expose-request-attributes=false
    spring.freemarker.expose-session-attributes=false
    spring.freemarker.expose-spring-macro-helpers=false
    #spring.freemarker.prefix=
    #spring.freemarker.request-context-attribute=
    #spring.freemarker.settings.*=
    spring.freemarker.suffix=.ftl
    spring.freemarker.template-loader-path=classpath:/templates/
    #comma-separated list
    #spring.freemarker.view-names= # whitelist of view names that can be resolved

    4、编写.ftl模型

    说明:我这里使用到的bootstrap-3.3.7方便好看 ,新建一个sysuser的视图层文件,保证良好的规则

    image

    <!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">
        <head>
            <meta content="text/html;charset=utf-8"></meta>
            <title>Hello World!</title>
            <script sec="../jquery-2.1.1/jquery.min.js"></script>
            <link rel="stylesheet" href="../bootstrap-3.3.7/dist/css/bootstrap.min.css"></link>
            <script sec="../bootstrap-3.3.7/dist/js/bootstrap.min.js"></script>
        </head>
    <body> 
    <div class="container">
            <table class="table">
                <caption>${sysUser}</caption>
                <thead>
                    <tr>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>User Name</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>aehyok</td>
                        <td>leo</td>
                        <td>@aehyok</td>
                    </tr>
                    <tr>
                        <td>lynn</td>
                        <td>thl</td>
                        <td>@lynn</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>

    freemarker的表达式可参考本博客【FreeMarker】随笔分类:http://www.cnblogs.com/xxt19970908/p/5594258.html

    四、启动测试

    1、检查启动信息:freemarker配置是否生效

    image

    2、访问:http://localhost:8080/sysUser

    image

  • 相关阅读:
    监听用户的访问的链接
    软件开发的流程
    PHP性能优化四(业务逻辑中使用场景)
    php性能优化三(PHP语言本身)
    php性能优化二(PHP配置php.ini)
    poj 1676
    计蒜客 蒜头君下棋
    计蒜客 划分整数
    计蒜客 蒜头君的数轴
    计蒜客 藏宝图
  • 原文地址:https://www.cnblogs.com/xxt19970908/p/6736455.html
Copyright © 2020-2023  润新知