zoukankan      html  css  js  c++  java
  • springboot模板引擎之模板整合之freemarker(二)

    1在pom.xml中添加freemark的依赖jar

    <!-- 引入freemark的模板引擎的依赖 -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>

    2在application.properties中添加配置文件

    #Freemarker的基础配置
    #是否开启thymeleaf缓存,本地为flase,生产改为true
    spring.freemarker.cache=false
    spring.freemarker.charset=UTF-8
    spring.freemarker.allow-request-override=false
    spring.freemarker.check-template-location=true

    #Freemarker类型
    spring.freemarker.content-type=text/html; charset=utf-8
    spring.freemarker.expose-request-attributes=true
    spring.freemarker.expose-session-attributes=true

    #Freemarker文件后缀
    spring.freemarker.suffix=.ftl
    #Freemarker路径
    spring.freemarker.template-loader-path=classpath:/templates/

    3在src/main/resources/templates下新建文件夹fm,fm文件夹下新建user和index.ftl

    如果未安装 freemarker ftl插件,可以看https://www.cnblogs.com/zhushilai/p/13565892.html

    index.ftl代码:


    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    haha,这是freemarker整合,index.html页面
    <h1>${serviceSetting.name}</h1>
    <h1>${serviceSetting.domain}</h1>
    </body>
    </html>

    4新建类ServiceSetting

     然后在application.properties中加入注解

     5新建freemarker包,包中新建FreeMarkerController类

    代码:

    package com.example.demo.controller.freemarker;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;

    import com.example.demo.domain.ServiceSetting;

    @Controller
    @RequestMapping("/freemarker")
    public class FreemarkerController {

    @Autowired
    private ServiceSetting serviceSetting;

    @GetMapping("hello")
    public String index(ModelMap modelMap) {
    modelMap.addAttribute("serviceSetting",serviceSetting);
    return "fm/index";
    }


    }

    7run  as application  ,访问地址:http://localhost:8080//freemarker/hello,即可取到对应的值

  • 相关阅读:
    lua 5.3最简单plugin编写
    CMake for MFC example
    写了个自动生成vcxproj的程序
    kindle試玩
    解放双手:如何在本地调试远程服务器上的Node代码
    PM2实用入门指南
    Express使用手记:核心入门
    Node服务一键离线部署
    fis-receiver:一行命令将项目部署到远程服务器
    Reflux系列01:异步操作经验小结
  • 原文地址:https://www.cnblogs.com/zhushilai/p/13566189.html
Copyright © 2011-2022 走看看