1、添加依赖

  org.springframework.boot

  spring-boot-starter-freemarker

  2、配置application.properties

  spring.freemarker.template-loader-path=classpath:/templates/

  spring.freemarker.charset=utf-8

  spring.freemarker.cache=false

  spring.freemarker.suffix=.ftl

  spring.freemarker.request-context-attribute=request

  3、创建资源目录

  resources 下创建 templates 目录,新增 index.ftl 文件

  ${name}

  4、编写控制器

  import org.springframework.stereotype.Controller;

  import org.springframework.ui.Model;

  import org.springframework.web.bind.annotation.RequestMapping;

  @Controller

  public class TestController {

  @RequestMapping(value = "/test")

  public String test(Model model){

  model.addAttribute("name", "admin");

  return "index";

  }

  }

  5、模板渲染工具类

  package com.vim.common.utils;

  import freemarker.template.Configuration;

  import freemarker.template.Template;

  import org.springframework.core.io.DefaultResourceLoader;

  import org.springframework.core.io.Resource;

  import java.io.*;

  import java.util.Map;

  public class FreemarkerUtils {

  /**

  * 使用模板字符串

  * @param templateString

  * @param model

  */

  public static String renderString(String templateString, Map model) {

  try {

  StringWriter result = new StringWriter();

  Template t = new Template("name", new StringReader(templateString), new Configuration());

  t.process(model, result);

  return result.toString();

  } catch (Exception e) {

  e.printStackTrace();

  }

  return null;

  }

  /**无锡人流医院哪家好 http://www.wxbhnkyy120.com/

  * 配置模板文件位置

  * @param directory

  * @return

  * @throws IOException

  */

  public static Configuration buildConfiguration(String directory) throws IOException {

  Configuration cfg = new Configuration(Configuration.VERSION_2_3_26);

  Resource path = new DefaultResourceLoader().getResource(directory);

  cfg.setDirectoryForTemplateLoading(path.getFile());

  return cfg;

  }

  /**

  * 使用模板文件

  * @param template

  * @param model

  */

  public static void renderTemplate(Template template, Map model, String saveFile) {

  try {

  FileWriter out = new FileWriter(new File(saveFile));

  template.process(model, out);

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

  6、注意事项

  freemarker 文件中的 js 引用一定要加闭合标签,且不能使用/>

Java -- springboot 配置 freemarker的更多相关文章

  1. SpringBoot入门-15(springboot配置freemarker使用YML)

    https://blog.csdn.net/fengsi2009/article/details/78879924 application.yml spring: http: encoding: fo ...

  2. springboot使用Freemarker继承

    最近需要用到Freemarker的继承.但是发现没有关于springboot配置Freemarker的继承的.所以趁现在有时间写个博客. 1. Freemarker继承介绍 Freemarker 通过 ...

  3. SpringBoot下配置FreeMarker配置远程模版

    需求产生原因 要求在同一个接口中,根据不同的参数,返回不同的视图结果 所有的视图中的数据基本一致 要求页面能静态化,优化SEO 例如:A接口返回客户的信息 客户A在调用接口时,返回其个性化定制的页面A ...

  4. springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息

    1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...

  5. 【转载】JAVA SpringBoot 项目打成jar包供第三方引用自动配置(Spring发现)解决方案

    JAVA SpringBoot 项目打成jar包供第三方引用自动配置(Spring发现)解决方案 本文为转载,原文地址为:https://www.cnblogs.com/adversary/p/103 ...

  6. springboot集成freemarker 配置application.properties详解

    #配置freemarker详解 #spring.freemarker.allow-request-override=false # Set whether HttpServletRequest att ...

  7. springboot集成freemarker属性配置(不知道是针对于某个版本,2.0后有变动)

    freemarker属性配置 freemarker属性配置: spring.freemarker.allow-request-override=false # 设置是否允许HttpServletReq ...

  8. 【Other】最近在研究的, Java/Springboot/RPC/JPA等

    我的Springboot框架,欢迎关注: https://github.com/junneyang/common-web-starter Dubbo-大波-服务化框架 dubbo_百度搜索 Dubbo ...

  9. 记springboot+mybatis+freemarker+bootstrap的使用(1)

    一..springboot的配置 1.安装并配置maven maven是项目管理工具,可以自动下载并管理jar包之间的依赖关系,可通过maven自动配置springboot 参照百度经验https:/ ...

随机推荐

  1. spring-boot 知识集锦

    1.spring-boot项目在外部tomcat环境下部署 https://blog.csdn.net/james_wade63/article/details/51009423 https://bl ...

  2. java中ExecutorService使用多线程处理业务

    ExecutorService executorService = Executors.newFixedThreadPool(5); List<CancelApprovalCallable> ...

  3. MQTT研究之EMQ:【CoAP协议应用开发】

    本博文的重点是尝试CoAP协议的应用开发,其中包含CoAP协议中一个重要的开源工具libcoap的安装和遇到的问题调研.当然,为了很好的将EMQ的CoAP协议网关用起来,也调研了下EMQ体系下,CoA ...

  4. C++内存管理3-探讨C++内存和回收

    1 C++内存对象大会战 如果一个人自称为程序高手,却对内存一无所知,那么我可以告诉你,他一定在吹牛. 用C或C++写程序,需要更多地关注内存,这不仅仅是因为内存的分配是否合理直接影响着程序的效率和性 ...

  5. 【NPDP笔记】第四章 文化组织与团队

    此为临时链接,仅用于预览,将在短期内失效.关闭 [NPDP笔记]第四章 文化组织与团队 小康 小康哥的产品之路 9月6日 4.1 文化和氛围对创新的重要性 文化:信念,价值观,假设,与期望 氛围:直接 ...

  6. GO语言 文件操作实例

    package main import ( "bufio" "fmt" "io/ioutil" "os" ) func ...

  7. 【转】Centos下编译升级安装Boost

    https://www.xingchenw.cn/article/191 Centos下编译升级安装Boost 首先在官网现在相应的包 https://www.boost.org/users/down ...

  8. linux 高级

    linux命令: top 查看整机的性能:   ----(看内存(mem)和cpu)   1:查看cpu的cpu的核数按1连续:   2:id=idle(空闲率),值越大越好,   3:load av ...

  9. PHP设计模式 - 状态模式

    状态模式当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类.状态模式主要解决的是当控制一个对象状态的条件表达式过于复杂时的情况.把状态的判断逻辑转移到表示不同状态的一系列类中,可以 ...

  10. netty例子-客户端每隔5秒发送查询时间的请求,服务器端响应请求

    netty是jboss公司开发的,基于异步的.事件驱动的网络应用程序框架,快速开发高性能.高可靠性的服务器和客户端程序 public class TimeServer { ; public void ...