• SpringBoot整合Freemarker


    1、在工程新建文件夹templates

    路径:src/main/resources/templates

    在配置文件application.properties中新增FreeMarker配置

    ########################################################
    ###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

    2、Freemarker的文件格式是.ftl

    3、pom.xml中引入依赖

    <!-- 引入freeMarker的依赖包. -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>

    4、创建一个controller接口,controller的注解要用@Controller【跳转页面】不能用@RestController【返回JSON数据】

      @RequestMapping("/index")
        public String index(Map<String, Object> map) {
            map.put("name","美丽的天使...");
           return "index";
        }

    5、写一个index.ftl文件

    <!DOCTYPE html>
    <html>
    <head lang="en">
    <meta charset="UTF-8" />
    <title>首页</title>
    </head>
    <body>
          ${name}
    <#if sex=="1"><#else if sex=="2"><#else>
            其他      
          
          </#if>      
         <#list userlist as user>
           ${user}
         </#list>
    </body> 
    </html>
  • 相关阅读:
    【Luogu】P2491消防(单调队列)
    【Luogu】P2824排序(二分答案+线段树排序)
    2-k8s笔记-Kubernetes安装配置指南
    1-k8s笔记-Kubernetes入门
    22-MySQL DBA笔记-其他产品的选择
    21-MySQL DBA笔记-高可用性
    20-MySQL DBA笔记-可扩展的架构
    19-MySQL DBA笔记-操作系统、硬件、网络的优化
    18-MySQL DBA笔记-MySQL Server调优
    17-MySQL DBA笔记-应用程序调优
  • 原文地址:https://www.cnblogs.com/yuhuiqing/p/10693113.html
Copyright © 2020-2023  润新知