Java-freemarker Templateloader Implementation Template

Source: Internet
Author: User
Tags assert

The realization of Templateloader

As a template file loading abstraction, naturally cannot restrict where the template comes from, in Freemarker by several major implementation classes to reflect, these templateloader can be used independently, WebApp need servlet environment. Of course you can achieve your own templateloader.

    • Stringtemplateloader directly puts the in-memory string object into and uses the
    • Filetemplateloader Local file directory
    • Classtemplateloader ClassPath Loading
    • Webapptemplateloader ServletContext
    • Multitemplateloader multiple Templateloader overlays, sequentially loading in order of the array

Stringtemplateloader

The use of memory strings.

@Test  Public void throws IOException {    new  stringtemplateloader ();     = "Rrrrr${key}rrrrr";    Stl.puttemplate ("Hello", template);     = Stl.findtemplatesource ("Hello");     = Stl.getreader (source, "Utf-8");     = ioutils.tostring (reader);    Assert.assertequals (template, dest);}

Cases:
     Public Static voidMain (string[] args) {
Read the file under Project Resource Resource=NewClasspathresource ("Sqls/xxx.sql"); InputStream InputStream=NULL; String SQL=NULL; Try{InputStream=Resource.getinputstream (); byte[] bytes =Ioutils.tobytearray (InputStream); SQL=NewString (bytes); SQL= Sql.replaceall ("\r\n\t", "" "); SQL= Sql.replaceall ("\ r \ n", "" "); SQL= Sql.replaceall ("\ T", "" "); SQL= Sql.replaceall ("[]+", "" ");
Replace parameter Map<String,Object> Parammap =NewHashmap<>(); Parammap.put ("Begindate", "20170901"); Parammap.put ("Futureenddate", "20170914"); Parammap.put ("TaskID", "8a7a8282600731bb016007324d770000"); List<SqlListEntity> cyclelist =NewArraylist<>(); Sqllistentity entity1=Newsqllistentity (); Entity1.setname ("1th Week"); Cyclelist.add (entity1); Sqllistentity Entity2=Newsqllistentity (); Entity2.setname ("2nd Week"); Cyclelist.add (Entity2); Parammap.put ("Cyclelist", cyclelist); Configuration Configuration=NewConfiguration (configuration.version_2_3_23);
Stringtemplateloader Stringtemplateloader=NewStringtemplateloader (); Stringtemplateloader.puttemplate (Sql,sql); Configuration.settemplateloader (Stringtemplateloader); Template Temp=configuration.gettemplate (SQL); StringWriter Querystringwriter=NewStringWriter (); Temp.process (Parammap, Querystringwriter); System.out.println (Querystringwriter.tostring ()); } Catch(IOException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); } finally{ioutils.closequietly (InputStream); } }

Multitemplateloader

Templateloader can be used in multiple types, the same type, and the order of the queries takes precedence in the order of the arrays.

@Test Public voidTESTMULTITL ()throwsIOException {Templateloader ctl=NewClasstemplateloader (templateloadertest.class,            "/"); Templateloader FTL1=NewFiletemplateloader (NewFile (System.getproperty ("User.dir"))); Multitemplateloader MTL=NewMultitemplateloader (Newtemplateloader[] {ftl1,ctl}); Object Source= Mtl.findtemplatesource ("TEST.FTL"); Reader Reader= Mtl.getreader (source, "Utf-8"); String dest=ioutils.tostring (reader); Assert.assertequals ("${hello}", dest);}

Typically used in configuration to easily handle Freemarker expressions
@Test Public voidTestinconfiguration ()throwsIOException {Configuration Configuration=NewConfiguration (configuration.version_2_3_21); Configuration.setdefaultencoding ("Utf-8"); Templateloader ctl=NewClasstemplateloader (templateloadertest.class,            "/"); Templateloader FTL1=NewFiletemplateloader (NewFile (System.getproperty ("User.dir"))); Multitemplateloader MTL=NewMultitemplateloader (Newtemplateloader[] {ftl1,ctl});    Configuration.settemplateloader (MTL); //configuration.gettemplate ("TEST.FTL"). Process (data, out);}

Other caches

Template loading is usually resource-intensive, the default is to open the cache, the cache implementation, whether to use the cache depends on your

Configuration.setcachestorage (new  freemarker.cache.NullCacheStorage ()); Configuration.cleartemplatecache ();

Java-freemarker Templateloader Implementation Template

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.