最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JAVA集成Freemarker生成靜態(tài)html過(guò)程解析

 更新時(shí)間:2020年06月08日 14:26:20   作者:受傷的芒果  
這篇文章主要介紹了JAVA集成Freemarker生成靜態(tài)html過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

Springboot

1.引入Freemarker jar包

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2.配置application.properties

### freemarker
spring.freemarker.templateLoaderPath=classpath:/templates/
spring.freemarker.suffix=.ftl
spring.freemarker.charset=UTF-8
spring.freemarker.request-context-attribute=request
spring.freemarker.settings.number_format=0.##########

3.創(chuàng)建ftl文件

在resource文件下新增文件夾templates,在templates文件夾下存放ftl文件,例如:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8"/>
  <title>FreeMarker</title>
</head>
<body>
<h1>Simple project</h1>
<h1>${key}</h1>
</body>
</html>

4.新建controller調(diào)用方法

@SuppressWarnings("unchecked")
  @RequestMapping(value="/test",method=RequestMethod.GET)
  public String test(Model model,HttpServletRequest request) {
     model.addAttribute("key","test project");
    return "test";
  }

5.生成html靜態(tài)文件

使用工具類:

/**
   * @param modeName 模板名稱
   * @param targetFileName 生成后的HTML名稱
   * @param params 傳入模板的參數(shù)
   * @Author: zy
   * @Date: 2020-6-4 09:39:47
   * @Description:生成靜態(tài)頁(yè)面
   */
  public void createHtmlByMode(String modeName, String targetFileName, Map<String, Object> params) {
    Writer out = null;
    // 找到服務(wù)器緩存目錄,可以自己指定目錄
    String folder = PropertisUtil.getApplicationProperties("healthReport.logs.urls") + targetFileName;
    // 通過(guò)匹配路徑格式拼接完整生成路徑
    String outFile = folder;
    try {
      File file = new File(outFile);
      // 生成空HTML文件
      if (!file.exists()) {
        file.createNewFile();
      }

      // 創(chuàng)建模版對(duì)象
      Template template = cfg.getTemplate(modeName);

      // 設(shè)置輸出流
      out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");// 設(shè)置編碼 UTF-8

      // 模版數(shù)據(jù)插入?yún)?shù),通過(guò)輸出流插入到HTML中
      template.process(params, out);

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (null != out) {
        try {
          out.flush();
          out.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }

修改controller中的方法:

@SuppressWarnings("unchecked")
  @RequestMapping(value="/test",method=RequestMethod.GET)
  public String test(Model model,HttpServletRequest request) {
     model.addAttribute("key","test project");
     //生成靜態(tài)文件
     Map param=new HashMap();
     param.put("key", "我是被生成的靜態(tài)文件");
     createHtmlByMode("test.ftl","test.html",param);
    return "test";
    
  }

實(shí)現(xiàn)效果(我這里默認(rèn)保存到d:/testlogs):

Springmvc(和springboot大致相同,此處只留下配置)

1.引入Freemarker jar包

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>3.2.4.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.25-incubating</version>
    </dependency>

2.springmvc配置

<!-- freemarker -->
  <bean id="freeMarkerConfigurer"
    class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/freemarker/ftl/"></property>
    <property name="defaultEncoding" value="utf-8" />
    <property name="freemarkerSettings">
      <props>
        <prop key="template_update_delay">1</prop>
        <prop key="locale">zh_CN</prop>
        <prop key="datetime_format">yyyy-MM-dd</prop><!-- 時(shí)間格式化 -->
        <prop key="date_format">yyyy-MM-dd</prop>
        <prop key="number_format">#.##</prop>
      </props>
    </property>
  </bean>

  <bean id="freeMarkerViewResolver"
    class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="cache" value="true" />
    <property name="prefix" value="" /><!-- 上面已經(jīng)配了,這里就不用配啦 -->
    <property name="suffix" value=".ftl" />
    <property name="contentType" value="text/html;charset=UTF-8" />
    <property name="allowSessionOverride" value="true" />
    <property name="allowRequestOverride" value="true" />
    <property name="exposeSpringMacroHelpers" value="true" />
    <property name="exposeRequestAttributes" value="true" />
    <property name="exposeSessionAttributes" value="true" />
    <property name="requestContextAttribute" value="request" />
  </bean>

3.調(diào)用方式

/**返回模板信息*/
 @SuppressWarnings("unchecked")
 @RequestMapping(value="/test",method={RequestMethod.GET})
 public ModelAndView test(HttpServletRequest request) {
   ModelAndView mv = new ModelAndView();
   //設(shè)置參數(shù)
   mv.addObject("key", "測(cè)試freemarker");
   //配置模板
   mv.setViewName("test");
   return mv;
 }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java技巧:快速獲取圖片拍攝時(shí)間

    Java技巧:快速獲取圖片拍攝時(shí)間

    想知道如何用Java讀取圖片的拍攝時(shí)間嗎?在這篇指南中,我將向你展示如何利用Java編程語(yǔ)言輕松獲取圖像的拍攝時(shí)間信息,不要錯(cuò)過(guò)這個(gè)簡(jiǎn)單而有用的技巧!
    2023-12-12
  • springboot解析自定義yml方式

    springboot解析自定義yml方式

    這篇文章主要介紹了springboot解析自定義yml方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Spring Cloud Alibaba Nacos Config進(jìn)階使用

    Spring Cloud Alibaba Nacos Config進(jìn)階使用

    這篇文章主要介紹了Spring Cloud Alibaba Nacos Config進(jìn)階使用,文中使用企業(yè)案例,圖文并茂的展示了Nacos Config的使用,感興趣的小伙伴可以看一看
    2021-08-08
  • SpringBoot實(shí)現(xiàn)接口防刷的五種方案

    SpringBoot實(shí)現(xiàn)接口防刷的五種方案

    接口防刷是保障系統(tǒng)安全與穩(wěn)定性的重要措施,惡意的高頻請(qǐng)求不僅會(huì)消耗服務(wù)器資源,還可能導(dǎo)致數(shù)據(jù)異常,甚至系統(tǒng)癱瘓,本文將介紹在SpringBoot框架下實(shí)現(xiàn)接口防刷的5種技術(shù)方案,需要的朋友可以參考下
    2025-04-04
  • SpringSession 請(qǐng)求與響應(yīng)重寫的實(shí)現(xiàn)

    SpringSession 請(qǐng)求與響應(yīng)重寫的實(shí)現(xiàn)

    這篇文章主要介紹了SpringSession 請(qǐng)求與響應(yīng)重寫的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • Spring?boot配置綁定和配置屬性校驗(yàn)的方式詳解

    Spring?boot配置綁定和配置屬性校驗(yàn)的方式詳解

    這篇文章主要介紹了Spring?boot配置綁定和配置屬性校驗(yàn),SpringBoot 提供了2 種方式進(jìn)行配置綁定,即使用 @ConfigurationProperties 注解和使用 @Value 注解,需要的朋友可以參考下
    2022-05-05
  • Apache?SkyWalking?監(jiān)控?MySQL?Server?實(shí)戰(zhàn)解析

    Apache?SkyWalking?監(jiān)控?MySQL?Server?實(shí)戰(zhàn)解析

    這篇文章主要介紹了Apache?SkyWalking?監(jiān)控?MySQL?Server?實(shí)戰(zhàn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • 關(guān)于Java Object你真的了解了嗎

    關(guān)于Java Object你真的了解了嗎

    下面小編就為大家?guī)?lái)一篇關(guān)于Java Object你真的了解了嗎。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • Spring Boot和Kotlin的無(wú)縫整合與完美交融

    Spring Boot和Kotlin的無(wú)縫整合與完美交融

    這篇文章主要給大家介紹了關(guān)于Spring Boot和Kotlin的無(wú)縫整合與完美交融的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • 查找jdk安裝路徑并且切換多版本jdk的詳細(xì)步驟

    查找jdk安裝路徑并且切換多版本jdk的詳細(xì)步驟

    在日常的工作學(xué)習(xí)中可能需要用到不同版本的jdk,下面這篇文章主要給大家介紹了關(guān)于查找jdk安裝路徑并且切換多版本jdk的詳細(xì)步驟,文中介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01

最新評(píng)論

当阳市| 龙海市| 南和县| 安吉县| 沂南县| 吉木乃县| 双峰县| 佛冈县| 洛扎县| 新龙县| 永川市| 怀集县| 金湖县| 武威市| 中宁县| 沽源县| 蓬安县| 泗洪县| 景洪市| 黎平县| 宜君县| 永丰县| 灵武市| 越西县| 松江区| 永胜县| 寿宁县| 靖江市| 柳州市| 新平| 云龙县| 西乌| 九龙县| 娄烦县| 西乡县| 福州市| 新平| 固镇县| 得荣县| 思茅市| 霍城县|