今天用Ajax异步添加评论,加载Freemarker模板引擎,生成模板模块

1.新建Freemarker模板

<li id="${comment.oId}">
<div>
<div class="avatar tooltipped tooltipped-n" aria-label="${comment.commentName}"
style="background-image: url(${comment.commentThumbnailURL})"></div>
<main>
<div class="fn-clear">
<#if "http://" == comment.commentURL>
${comment.commentName}
<#else>
<a class="user-name" href="${comment.commentURL}" target="_blank">${comment.commentName}</a>
</#if>
<#if comment.isReply>
@<a class="user-name" href="/${article.articlePermalink}#${comment.commentOriginalCommentId}"
onmouseover="page.showComment(this, '${comment.commentOriginalCommentId}', 23);"
onmouseout="page.hideComment('${comment.commentOriginalCommentId}')"
>${comment.commentOriginalCommentName}</a>
</#if>
<time class="ft-gray">${comment.commentDate?string("yyyy-MM-dd HH:mm")}</time> <#if article.articleCommentable==1>
<a class="reply-btn" href="javascript:replyTo('${comment.oId}')">${replyLabel}</a>
</#if>
</div>
<div class="content-reset">
${comment.commentContent}
</div>
</main>
</div>
</li>

2.新建FreemarkerUtils工具类

package com.fdzang.mblog.utils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler; import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map; public class FreemarkerUtils {
public static String getTemplate(String template, Map<String,Object> map) throws IOException, TemplateException {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
String templatePath = FreemarkerUtils.class.getResource("/").getPath()+"/templates";
cfg.setDirectoryForTemplateLoading(new File(templatePath));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
cfg.setWrapUncheckedExceptions(true);
Template temp = cfg.getTemplate(template);
StringWriter stringWriter = new StringWriter();
temp.process(map, stringWriter);
return stringWriter.toString();
}
}

template为模板的名称,Map为需要插入的参数

关于加载模板位置的方法,借鉴于

https://blog.csdn.net/gtlishujie/article/details/52300381

我就不多加累述了,关键在于获取文件路径,文件路径不对的话,可以试着输出然后调试,个人推荐文件加载这个方法

3.Controller层的方法

Map<String,Object> map=new HashMap<>();
map.put("article",article);
map.put("comment",comment);
map.put("replyLabel","回复");
String cmtTpl= FreemarkerUtils.getTemplate("common-comment.ftl",map);
result.setCmtTpl(cmtTpl);

定义好Map参数,指定加载的模板引擎,就可以得到解析后的HTML了

SpringBoot获取Freemarker模板引擎,生成HTML代码的更多相关文章

  1. springboot整合freemarker模板引擎后在页面获取basePath绝对路径

    在项目中引用静态资源文件或者进行ajax请求时我们有时候会使用 ${basePath} ,其实这就是一种获取绝对路径的方式: 那么在springboot项目中要怎么配置才能使用 basePaht呢? ...

  2. 使用freemarker模板引擎生成word文档的开发步骤

    1.准备模板文档,如果word文档中有表格,只保留表头和第一行数据:2.定义变量,将word文档中的变量用${var_name}替换:3.生成xml文件,将替换变量符后的word文档另存为xml文件: ...

  3. Spring Boot 揭秘与实战(七) 实用技术篇 - FreeMarker 模板引擎

    文章目录 1. FreeMaker 代替 JSP 作为页面渲染 2. 生成静态文件 3. 扩展阅读 4. 源代码 Spring Boot 提供了很多模板引擎的支持,例如 FreeMarker.Thym ...

  4. SpringBoot日记——Thymeleaf模板引擎篇

    开发通常我们都会使用模板引擎,比如:JSP.Velocity.Freemarker.Thymeleaf等等很多,那么模板引擎是干嘛用的? 模板引擎,顾名思义,是一款模板,模板中可以动态的写入一些参数, ...

  5. Java之利用Freemarker模板引擎实现代码生成器,提高效率

    https://blog.csdn.net/huangwenyi1010/article/details/71249258  java模板引擎freemarker代码生成器 更多 个人分类: 一步一步 ...

  6. Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎

    前面几篇介绍了返回json数据提供良好的RESTful api,下面我们介绍如何把处理完的数据渲染到页面上. Spring Boot 使用模板引擎 Spring Boot 推荐使用Thymeleaf. ...

  7. FreeMarker模板引擎

    现在开发的项目,也是基于SOA架构,每个功能接口都是用WebService实现,Web服务的通信协议就是用XML来传输. 以前写WebService都是自动生成XML,没想到这项目竟然要自己定义XML ...

  8. freemarker模板引擎的使用

    freemarker是一套前端模板引擎,在使用时,要先在web项目中添加freemarker.jar的依赖. 我在这里主要演示spring-mvc整合freemarker模板引擎.项目案例的文件包结构 ...

  9. Spring Boot☞ 使用freemarker模板引擎渲染web视图

    效果图 代码 package com.wls.integrateplugs.hello.controller; /** * Created by wls on 2017/8/24. */ import ...

随机推荐

  1. MySql注释的写法

    每一种语言都有它的注释方式,代码量少的时候还可以,随着代码量越来越多,代码注释的重要性也越发凸显. 在mysql中主要有三种方式: 1.常用的方式,跟在css中那些注释一样 :/* 内容 */ /* ...

  2. Oracle统计、分析和优化环境配置

    Oracle统计.分析和优化环境配置 创建批处理文件Login.bat 用于初始化设置系统环境 Login.bat @echo off title eoda mode con cols=140 col ...

  3. java中将对象引用设置为null对于GC有没有帮助

    相信,网上很多java性能优化的帖子里都会有这么一条: 尽量把不使用的对象显式得置为null.这样有助于内存回收 可以明确的说,这个观点是基本错误的.sun jdk远比我们想象中的机智.完全能判断出对 ...

  4. # python04---函数

    python04---函数 一. 初识函数 """ def 函数名(参数): 函数体 返回值 """ # def: 定义函数关键字 # 函数 ...

  5. Python学习日记(六) 浅深copy

    浅深copy即完全复制一份和部分复制一份 浅深copy在列表数据量较大时不建议使用,比较消耗内存资源 1.赋值运算 l1 = [1,'s',[1,2,3]] l2 = l1 print(id(l1), ...

  6. Keras实现Self-Attention

    本文转载自:https://blog.csdn.net/xiaosongshine/article/details/90600028 一.Self-Attention概念详解 对于self-atten ...

  7. mysql字典取值,列表包含

    SELECT * FROM e where JSON_CONTAINS(json_extract(scope_detail, '$.shop'), '001');

  8. String/StringBuffer

    1. 将String中的空格替换成 %20 public class ReplaceBlank { public static void main(String[] args) { String st ...

  9. 《你们都是魔鬼吗》第八次团队作业:第三天Alpha冲刺

    <你们都是魔鬼吗>第八次团队作业:Alpha冲刺 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 你们都是魔鬼吗 作业学习目标 完成最 ...

  10. NPOI - .NET Excel & Word操作组件

    概要 NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. NPOI是构建在POI 3.x版本之上的,它可以在没有安装Offi ...