zoukankan      html  css  js  c++  java
  • Java -- springboot 配置 freemarker

      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 引用一定要加闭合标签,且不能使用/>

  • 相关阅读:
    [SSRS] Use Enum values in filter expressions Dynamics 365 Finance and Operation
    Power shell deploy all SSRS report d365 FO
    display method in Dynamics 365 FO
    How To Debug Dynamics 365 Finance and Operation
    Computed columns and virtual fields in data entities Dynamics 365
    Azure DevOps for Power Platform Build Pipeline
    Create readonly entities that expose financial dimensions Dynamics 365
    Dataentity call stack dynamics 365
    Dynamics 365 FO extension
    Use singletenant servertoserver authentication PowerApps
  • 原文地址:https://www.cnblogs.com/djw12333/p/11275021.html
Copyright © 2011-2022 走看看