FreeMarer 模板加載,使用FreeMarker加載遠程主機上模板文件
FreeMarker加載模板文件的三種方式:
1、從文件目錄加載
2、從類路徑加載
3、從Servlet上下文加載
其中第二個和第三個常用在Web開發環境中,類路徑也會使用在普通的Java Project中,
如果模板文件不是和應用程序放在同一台主機上,那么如何去讀取和解析這些模板文件呢?答案是可以解決的,FreeMarker就提供給一種加載模板的方式,查看API就有URLTemplateLoader類,該類為抽象類,從名字就可以看出從給定的URL加載模板文件,這個URL並沒有限定來源,
來源可實現從FTP服務器,Hadoop,db等等。那么可以自定義個加載器,從這個類繼承,實現里面的getUrl方法即可:
/**
* 自定義遠程模板加載器,用來加載遠程機器上存放的模板文件.HTTP
*
* @author wanglijun
*
*/
public class RemoteTemplateLoader extends URLTemplateLoader {
// 遠程模板文件的存儲路徑(目錄)
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;
}
}
Spring MVC配置文件如下:
<!-- 針對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" />
<!--編碼 -->
<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/" />
<!--設置遠程加載路徑-->
<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" />
<!-- 模板加載路徑 -->
<property name="templateLoaderPaths">
<list>
<value>/WEB-INF/views/</value>
<value>classpath:/views/</value>
</list>
</property>
<property name="defaultEncoding" value="utf-8" />
</bean>
需要注意的是:通過遠程加載模板,FreeMarker沒有check遠程加載此文件是否存在,所以只能通過本地列表(includePaths)判斷是否存在,加快模板加載速度。
另外也通過http和FTP方式判斷此模板是否遠程服務器存在,但考慮性能及文件加載數量(主要是加載一些公共頭信息文件,文件數據量有限)
通過此擴展功能可實現(header.ftl、footer.ftl及公共模塊的管理),在項目中可實現多個WEB網絡實現統一的管理。