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了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM