Freemarer template loading: Use freemarker to load template files on the remote host

Source: Internet
Author: User

Freemarer template loading: Use freemarker to load template files on the remote host

Freemarker can load template files in three ways:

1. load from the file directory

2. load from the class path

3. Load from servlet context

The second and third are commonly used in the Web development environment, and the class path is also used in common Java projects,

If the template file is not stored on the same host as the application, how do I read and parse these template files? The answer is yes. freemarker provides a method for loading templates. The urltemplateloader class is available for viewing APIs. This class is an abstract class. The name indicates that the template file is loaded from the given URL, this URL does not specify the source,

Source can be implemented from the FTP server, hadoop, DB, and so on. You can customize a loader to inherit from this class and implement the geturl method in it:

/**
* The Custom remote template loader is used to load the template files stored on the remote machine. HTTP
*
* @ Author wanglijun
*
*/
Public class remotetemplateloader extends urltemplateloader {
// Storage path (directory) of the remote Template File)
Private string remotepath;

Private list <string> includepaths;

Public remotetemplateloader (string remotepath ){
If (remotepath = NULL ){
Throw new illegalargumentexception ("remotepath is null ");
}
This. remotepath = canonicalizeprefix (remotepath );
If (this. remotepath. indexof ('/') = 0 ){
This. remotepath = This. remotepath. substring (this. remotepath. indexof ('/') + 1 );
}
}

@ Override
Public object findtemplatesource (string name) throws ioexception {
If (this. includepaths! = NULL & this. includepaths. Contains (name )){
Return super. findtemplatesource (name );
}
Return NULL;

}

@ Override
Protected URL geturl (string name ){
// Name = Name. Replace ("_ ZH ","");
String fullpath = This. remotepath + name;
System. Out. println (fullpath );
If (this. remotepath. Equals ("/"))&&(! Isschemeless (fullpath ))){
Return NULL;
}

URL url = NULL;
Try {
Url = new URL (fullpath );
} Catch (malformedurlexception e ){
E. printstacktrace ();
}
Return URL;
}

Private Static Boolean isschemeless (string fullpath ){
Int I = 0;
Int Ln = fullpath. Length ();

If (I <ln) & (fullpath. charat (I) = '/'))
I ++;

While (I <ln ){
Char c = fullpath. charat (I );
If (C = '/')
Return true;
If (C = ':')
Return false;
I ++;
}
Return true;
}

Public void setremotepath (string remotepath ){
This. remotepath = remotepath;
}

Public list <string> getincludepaths (){
Return includepaths;
}

Public void setincludepaths (list <string> includepaths ){
This. includepaths = includepaths;
}
}

 

 

The spring MVC configuration file is as follows:

<! -- View for free marker -->
<Bean id = "viewresolver"
Class = "org. springframework. Web. servlet. View. freemarker. freemarkerviewresolver">
<Property name = "cache" value = "false"/>
<Property name = "order" value = "1"/>
<Property name = "viewclass"
Value = "org. springframework. Web. servlet. View. freemarker. freemarkerview"/>
<! -- <Property name = "viewnames"> <array> <value> *. FTL </value> </array>
</Property> -->
<Property name = "requestcontextattribute" value = "request"/>
<Property name = "exposespringmacrohelpers" value = "true"/>
<Property name = "exposerequestattributes" value = "true"/>
<Property name = "exposesessionattributes" value = "true"/>
<Property name = "allowsessionoverride" value = "true"/>
<! -- Encoding -->
<Property name = "contenttype" value = "text/html; charset = UTF-8"/>
</Bean>

 

<Bean id = "freemarkerconfigurer"
Class = "org. springframework. Web. servlet. View. freemarker. freemarkerconfigurer">
<Property name = "configuration" ref = "freemarkerconfiguration"/>
</Bean>
<Bean id = "remotetemplateloader" class = "com. SAIC. Demo. Web. remotetemplateloader">
<Constructor-Arg name = "remotepath" value = "http: // 192.168.1.20: 9090/"/>

<! -- Set the remote loading path -->

<Property name = "includepaths">
<List>
<Value> footer. FTL </value>
<Value> common/footer. FTL </value>
</List>
</Property>
</Bean>

<Util: List id = "pretemplateloaders" list-class = "Java. util. arraylist"
Value-type = "com. SAIC. Demo. Web. remotetemplateloader">
<Ref bean = "remotetemplateloader"/>
</Util: List>

<Bean id = "freemarkerconfiguration"
Class = "org. springframework. UI. freemarker. freemarkerconfigurationfactorybean">
<Property name = "posttemplateloaders" ref = "pretemplateloaders"/>
<! -- Template loading path -->
<Property name = "templateloaderpaths">
<List>
<Value>/WEB-INF/views/</value>
<Value> classpath:/views/</value>
</List>
</Property>
<Property name = "defaultencoding" value = "UTF-8"/>
</Bean>

 

 

Note that freemarker does not check whether the file exists remotely by remotely loading the template. Therefore, you can only use the local list (includepaths) to determine whether the file exists and speed up template loading.

In addition, you can determine whether the template exists on a remote server through HTTP or FTP, but consider the performance and number of file loads (mainly loading some public header information files with limited file data volume)

With this extension function, you can manage headers. FTL, footer. FTL, and public modules. In the project, you can manage multiple web networks in a unified manner.

Freemarer template loading: Use freemarker to load template files on the remote host

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.