在一个大网站里,有很多子域名,也就是有很多子系统,这些子系统由不同的团队负责,对整个网站的风格的风格至少得要是一致的(最基本的页头、页尾必须一致),这个时候得提供一份统一的页头、页尾以及公共的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. 简明 MongoDB 入门教程

    MongoDB 是免费开源的跨平台 NoSQL 数据库,命名源于英文单词 humongous,意思是「巨大无比」,可见开发组对 MongoDB 的定位.与关系型数据库不同,MongoDB 的数据以类似 ...

  2. SpringBoot 使用Swagger2打造在线接口文档(附汉化教程)

    原文地址: https://www.jianshu.com/p/7e543f0f0bd8 SpringBoot + Swagger2 UI界面-汉化教程 1.默认的英文界面UI 想必很多小伙伴都曾经使 ...

  3. hdu 2544 单源最短路问题 dijkstra+堆优化模板

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  4. CF 329B(Biridian Forest-贪心-非二分)

    B. Biridian Forest time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Spring系列:Scheduled注解学习笔记

    一.试验代码 //@Scheduled(fixedRate = 5000) //@Scheduled(fixedDelay = 5000) @Scheduled(cron ="*/5 * * ...

  6. C#微信接口之推送模板消息功能示例

    本文实例讲述了C#微信接口之推送模板消息功能.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2 ...

  7. python2.7 调用 .net的webservice asmx

    首先安装pip install suds 或下载:https://pypi.org/project/suds-jurko/0.6/#files 这个是最新版本 由于不支持python3.6, 所以只能 ...

  8. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十二)VMW安装四台CentOS,并实现本机与它们能交互,虚拟机内部实现可以上网。

    Centos7出现异常:Failed to start LSB: Bring up/down networking. 按照<Kafka:ZK+Kafka+Spark Streaming集群环境搭 ...

  9. [Angular] Use Angular components in AngularJS applications with Angular Elements

    When migrating AngularJS (v1.x) applications to Angular you have different options. Using Angular El ...

  10. PHPCMS标签

    获取栏目名:{$CAT[catname]}或{$catname}获取父栏目名:{$CATEGORYS[$CAT[parentid]][catname]} 获取同级栏目列表: {pc:content a ...