SpringBoot获取Freemarker模板引擎,生成HTML代码
今天用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代码的更多相关文章
- springboot整合freemarker模板引擎后在页面获取basePath绝对路径
在项目中引用静态资源文件或者进行ajax请求时我们有时候会使用 ${basePath} ,其实这就是一种获取绝对路径的方式: 那么在springboot项目中要怎么配置才能使用 basePaht呢? ...
- 使用freemarker模板引擎生成word文档的开发步骤
1.准备模板文档,如果word文档中有表格,只保留表头和第一行数据:2.定义变量,将word文档中的变量用${var_name}替换:3.生成xml文件,将替换变量符后的word文档另存为xml文件: ...
- Spring Boot 揭秘与实战(七) 实用技术篇 - FreeMarker 模板引擎
文章目录 1. FreeMaker 代替 JSP 作为页面渲染 2. 生成静态文件 3. 扩展阅读 4. 源代码 Spring Boot 提供了很多模板引擎的支持,例如 FreeMarker.Thym ...
- SpringBoot日记——Thymeleaf模板引擎篇
开发通常我们都会使用模板引擎,比如:JSP.Velocity.Freemarker.Thymeleaf等等很多,那么模板引擎是干嘛用的? 模板引擎,顾名思义,是一款模板,模板中可以动态的写入一些参数, ...
- Java之利用Freemarker模板引擎实现代码生成器,提高效率
https://blog.csdn.net/huangwenyi1010/article/details/71249258 java模板引擎freemarker代码生成器 更多 个人分类: 一步一步 ...
- Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎
前面几篇介绍了返回json数据提供良好的RESTful api,下面我们介绍如何把处理完的数据渲染到页面上. Spring Boot 使用模板引擎 Spring Boot 推荐使用Thymeleaf. ...
- FreeMarker模板引擎
现在开发的项目,也是基于SOA架构,每个功能接口都是用WebService实现,Web服务的通信协议就是用XML来传输. 以前写WebService都是自动生成XML,没想到这项目竟然要自己定义XML ...
- freemarker模板引擎的使用
freemarker是一套前端模板引擎,在使用时,要先在web项目中添加freemarker.jar的依赖. 我在这里主要演示spring-mvc整合freemarker模板引擎.项目案例的文件包结构 ...
- Spring Boot☞ 使用freemarker模板引擎渲染web视图
效果图 代码 package com.wls.integrateplugs.hello.controller; /** * Created by wls on 2017/8/24. */ import ...
随机推荐
- HttpClinet工具类
一.URL调用 忽略https证书 1.调用 InputStream in = null; try { URL url = new URL( "url地址" ); IgnoreSS ...
- uboot使用脚本
使用mkimage命令 # mkimage -A ARM -O linux -T script -C none -n "script" -d *.sh *.img # tftp x ...
- 聊Java中的任务调度的实现方法及比较
前言 任务调度是指基于给定时间点,给定时间间隔或者给定执行次数自动执行任务.本文由浅入深介绍四种任务调度的 Java 实现: Timer ScheduledExecutor 开源工具包 Quartz ...
- table标签修改tr,td标签的行距
修改tr标签的行距,tbale标签的td标签间距 看如下Css文件的代码,其都没有table的tr行距产生效果 tr{ margin-top: 10px; padding: 10px; } td{ m ...
- Jquery简单闭包
<html> <body> <script src="Js/Index.js"></script> <script type= ...
- yolov3---报"段错误"的可能原因及解决方法
参考: ************************************************************************************************ ...
- 零基础Python教程-详说list有序集合
list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出你周围同事的名字,就可以用一个list表示: >>> classmates = ['Michael', 'Bob' ...
- Linux 服务器性能出问题,排查下这些参数指标
taozj马哥Linux运维 一个基于 Linux 操作系统的服务器运行的同时,也会表征出各种各样参数信息.通常来说运维人员.系统管理员会对这些数据会极为敏感,但是这些参数对于开发者来说也十分重要,尤 ...
- 《少年先疯队》第九次团队作业:Beta冲刺与团队项目验收
博文简要信息表: 项目 内容 软件工程 https://www.cnblogs.com/nwnu-daizh/ 本次实验链接地址 https://www.cnblogs.com/nwnu-daizh/ ...
- 微信小程序引入Vant组件库
前期准备 Vant Weapp组件库:https://youzan.github.io/vant-weapp/#/intro 1.先在微信开发者工具中打开项目的终端: 然后初始化一个package.j ...