Springboot get freemarker template engine, generate HTML code

Source: Internet
Author: User

Add a comment asynchronously today with Ajax, load the Freemarker template engine, generate a template module

1. Create a new Freemarker template
<LiID= "${comment.oid}">    <Div>        <Divclass= "Avatar tooltipped tooltipped-n"Aria-label= "${comment.commentname}"style= "Background-image:url (${comment.commentthumbnailurl})"></Div>        <Main>            <Divclass= "Fn-clear">                <#if"http //"== Comment.commenturl>${comment.commentname}<#else>                <aclass= "User-name"href= "${comment.commenturl}"Target= "_blank">${comment.commentname}</a>                </#if>                <#ifcomment.isreply>                @<aclass= "User-name"href= "/${article.articlepermalink}#${comment.commentoriginalcommentid}"onmouseover= "Page.showcomment (this, ' ${comment.commentoriginalcommentid} ',");onmouseout= "Page.hidecomment (' ${comment.commentoriginalcommentid} ')"                >${comment.commentoriginalcommentname}</a>                </#if>                < Timeclass= "Ft-gray">${comment.commentdate?string ("Yyyy-mm-dd hh:mm")}</ Time>                <#ifarticle.articlecommentable==1>                    <aclass= "Reply-btn"href= "Javascript:replyto (' ${comment.oid} ')">${replylabel}</a>                </#if>            </Div>            <Divclass= "Content-reset">${comment.commentcontent}</Div>        </Main>    </Div></Li>
2. New Freemarkerutils Tool Class
 Packagecom.fdzang.mblog.utils;Importfreemarker.template.Configuration;Importfreemarker.template.Template;Importfreemarker.template.TemplateException;ImportFreemarker.template.TemplateExceptionHandler;ImportJava.io.File;Importjava.io.IOException;ImportJava.io.StringWriter;ImportJava.util.Map; Public classFreemarkerutils { Public StaticString GetTemplate (string template, Map<string,object> Map)throwsIOException, templateexception {Configuration cfg=NewConfiguration (configuration.version_2_3_28); String TemplatePath= Freemarkerutils.class. GetResource ("/"). GetPath () + "/templates"; Cfg.setdirectoryfortemplateloading (NewFile (TemplatePath)); Cfg.setdefaultencoding ("UTF-8");        Cfg.settemplateexceptionhandler (Templateexceptionhandler.rethrow_handler); Cfg.setlogtemplateexceptions (false); Cfg.setwrapuncheckedexceptions (true); Template Temp=cfg.gettemplate (template); StringWriter StringWriter=NewStringWriter ();        Temp.process (map, StringWriter); returnstringwriter.tostring (); }}

Template is the name of the stencil, and map is the parameter that needs to be inserted

The method of loading the template location, drawing on

52300381

I will not more tired, the key is to get the file path, the file path is not correct, you can try to output and then debug, personal recommendation file loading this method

method of 3.Controller layer
map<string,object> map=New hashmap<>() map.put ("article", article); Map.put ( "comment", comment), Map.put ("Replylabel", "Reply"); String Cmttpl= freemarkerutils.gettemplate ("COMMON-COMMENT.FTL", map); Result.setcmttpl (CMTTPL);

Define the map parameter, specify the loaded template engine, and you can get the parsed HTML.

Springboot get freemarker template engine, generate HTML code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.