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