在一个大网站里,有很多子域名,也就是有很多子系统,这些子系统由不同的团队负责,对整个网站的风格的风格至少得要是一致的(最基本的页头、页尾必须一致),这个时候得提供一份统一的页头、页尾以及公共的JS、css等内容,但如果是直接给源代码(ftl/js/css)的形式,对于后期的升级维护必然增加不必要的麻烦,必须得只有一个维护这个代码。

freemarker提供了远程模板加载的功能,在各个业务方里就像使用本地的模板一样使用远程的统一的模板代码。

1、编写自定义的模板加载器(继续freemarker的接口或者抽象类)

上图中RemoteTemplateLoader是我们实现的freemarker的URLTemplateLoader抽象类的远程模板加载类。

代码如下:

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List; import org.apache.commons.lang3.StringUtils; import freemarker.cache.URLTemplateLoader; /**
* 自定义远程模板加载器,用来加载远程机器上存放的模板文件.HTTP
*
* @author Administrator
*
*/
public class RemoteTemplateLoader extends URLTemplateLoader {
// 远程模板文件的存储路径(目录)
private String remotePath; private List<String> includePaths; private String paths; 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;
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 void setPaths(String paths) {
this.paths = paths;
if (StringUtils.isNotEmpty(this.paths)) {
String [] s = this.paths.split(";");
this.includePaths = Arrays.asList(s);
}
}
}

2、在springMVC XML里配置该RemoteTemplateLoader

<bean id="remoteTemplateLoader" class="com.xxx.RemoteTemplateLoader">
<constructor-arg name="remotePath" value="http://10.1.1.1/" />
<property name="paths" value="/test/a.ftl;/test/b.ftl" />
</bean> <bean id="remoteTemplateLoader2" class="com.xxx.RemoteTemplateLoader">
<constructor-arg name="remotePath" value="http://103.11.5.10/" />
<property name="paths" value="/test/a.ftl;/test/b.ftl" />
</bean> <util:list id="preTemplateLoaders" list-class="java.util.ArrayList" value-type="com.xxx.RemoteTemplateLoader">
<ref bean="remoteTemplateLoader" />
<ref bean="remoteTemplateLoader2" />
</util:list> <bean id="freeMarkerConfigurer"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<description>Required for Freemarker to work in web tier</description>
<property name="configuration" ref="freemarkerConfiguration" />
</bean> <bean id="freemarkerConfiguration"
class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
<description>Using the Config directly so we can use it outside the
web tier</description>
<property name="preferFileSystemAccess" value="true"/>
<property name="postTemplateLoaders" ref="preTemplateLoaders" />
<!-- 模板加载路径 -->
<property name="templateLoaderPaths">
<list>
<value>/WEB-INF/views</value>
</list>
</property>
<property name="configLocation">
<value>classpath:conf/freemarker.properties</value>
</property>
  ...................
</bean>

3、在自己的FTL文件中include该远程模板

<#include "/test/a.ftl">

这个上面的url中是在RemoteTemplateLoader的XML的参数里已经进行了配置,所以跟使用本地的ftl是一样的。

springMVC加载远程freemarker模板文件的更多相关文章

  1. 钓鱼攻击之远程加载恶意Word模版文件上线CS

    0x00 前言 利用Word文档加载附加模板时的缺陷所发起的恶意请求而达到的攻击目的,所以当目标用户点开攻击者发给他的恶意word文档就可以通过向远程服务器请求恶意模板并执行恶意模板上的恶意代码.这里 ...

  2. SpringMVC加载配置Properties文件的几种方式

    最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大, 网上介绍Controller参数绑定.URL映射的文章都很多了,写这篇博客主要总结一下 ...

  3. AIRSDK 3.7 加载远程的含有代码的swf文件

    之前就说这个版本会解决可以加载远程的含有代码的swf文件的需求.但是,一直比较好奇这个是否行得通,还以为 Adobe 副总裁去了苹果,内部给了特殊待遇. 因为苹果一直就是不允许远程加载代码的,像js文 ...

  4. XSS漏洞之加载远程js文件

    这次在对一个系统渗透测试过程中,发现一个XSS漏洞,可弹窗,并且没有httponly 但是在尝试加载远程js文件的时候发现,script标签被过滤掉了,准确的说应该是服务器后端在识别到输入内容有< ...

  5. 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件

    [源码下载] 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件 作者 ...

  6. iOS Cordova 加载远程界面

    老大说,我们的项目要hybrid,要实现1.html能调用native:2.本地html调用本地html界面:3.能加载远程界面..... 因为我的项目是已有的(以下简称 项目),所以是要在已有的项目 ...

  7. 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件

    CustomResource ResourceDictionary 加载外部的 ResourceDictionary 文件 示例1.演示“CustomResource”相关知识点Resource/Cu ...

  8. freemarker模板文件的4个组成部分

    FreeMarker模板文件主要由以下4个部分组成:1.文本,直接输出的部分.2.注释,即<#–…–>格式不会输出.3.插值(Interpolation):即${..}或者#{..}格式的 ...

  9. DevExpress XtraReport - 动态加载报表布局模板

    XtraReport的报表模板文件是.repx,下面的代码演示动态加载报表布局模板. XtraReport mReport = new XtraReport(); mReport.LoadLayout ...

随机推荐

  1. Prometheus 初体验

    本文环境 Redhat Linux 6.7, Prometheus 2.2.1,node_exporter 1.5.2 介绍 Prometheus 是2012年由 SoundCloud 开源的系统监控 ...

  2. HipHop PHP & HHVM资料收集

    百度百科 HipHop PHP实战(详解web运行模式) 百度 PHP7和HHVM的性能之争

  3. IE9版本以下ajax 跨域问题解决

    ajax跨域请求数据在谷歌火狐我本地IE11都是没问题的. 让测试就发现问题了,IE8下请求不到数据,然后我查看一下自己写的js看有没有不兼容问题,可是都没有啊,为什么就请求不到呢. 我把ajax的e ...

  4. POJ 1719 Shooting Contest(二分图匹配)

    POJ 1719 Shooting Contest id=1719" target="_blank" style="">题目链接 题意:给定一个 ...

  5. Android Animatioin总结

    一.动画分类 1.  View Animation (Tween动画)  执行一系列简单的转换.      针对 视图对象内容进行移动,放大,缩小以及产生透明度的变化等四种动画操作.仅针对视图对象内容 ...

  6. kendoui仪表盘和柱状图 示例

    一说到kendeodui我相信大家一定不陌生,这套js在画图方面效果也不错. 现在来看一看 仪表盘和柱状图的效果吧: html和js代码如下: <!DOCTYPE html> <ht ...

  7. 文本框只能输入数字(兼容IE火狐)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. MyBatis动态SQL foreach标签实现批量插入

    需求:查出给定id的记录: <select id="getEmpsByConditionForeach" resultType="com.test.beans.Em ...

  9. ztree默认自动打开第一级

    var treeObj = $.fn.zTree.getZTreeObj("tree"); var nodes = treeObj.getNodes(); if (nodes.le ...

  10. PLSQL Developer连接远程Oracle数据库

    要连接远程数据库,传统的一定可行的方法是在本地装一个oracle.然后使用"Network Configuration Assistant"配置.之后用PL/SQL Dev连接.由 ...