`
san_yun
  • 浏览: 2696978 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

servlet 整合freemarkder

 
阅读更多

 

Servlet与FreeMarker整合

http://oma1989.iteye.com/blog/1421804

 

SpringTemplateLoader

 

public class SpringTemplateLoader implements TemplateLoader {

	protected final Log logger = LogFactory.getLog(getClass());

	private final ResourceLoader resourceLoader;

	private final String templateLoaderPath;


	/**
	 * Create a new SpringTemplateLoader.
	 * @param resourceLoader the Spring ResourceLoader to use
	 * @param templateLoaderPath the template loader path to use
	 */
	public SpringTemplateLoader(ResourceLoader resourceLoader, String templateLoaderPath) {
		this.resourceLoader = resourceLoader;
		if (!templateLoaderPath.endsWith("/")) {
			templateLoaderPath += "/";
		}
		this.templateLoaderPath = templateLoaderPath;
		if (logger.isInfoEnabled()) {
			logger.info("SpringTemplateLoader for FreeMarker: using resource loader [" + this.resourceLoader +
					"] and template loader path [" + this.templateLoaderPath + "]");
		}
	}

	public Object findTemplateSource(String name) throws IOException {
		if (logger.isDebugEnabled()) {
			logger.debug("Looking for FreeMarker template with name [" + name + "]");
		}
		Resource resource = this.resourceLoader.getResource(this.templateLoaderPath + name);
		return (resource.exists() ? resource : null);
	}

	public Reader getReader(Object templateSource, String encoding) throws IOException {
		Resource resource = (Resource) templateSource;
		try {
			return new InputStreamReader(resource.getInputStream(), encoding);
		}
		catch (IOException ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("Could not find FreeMarker template: " + resource);
			}
			throw ex;
		}
	}


	public long getLastModified(Object templateSource) {
		Resource resource = (Resource) templateSource;
		try {
			return resource.lastModified();
		}
		catch (IOException ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("Could not obtain last-modified timestamp for FreeMarker template in " +
						resource + ": " + ex);
			}
			return -1;
		}
	}

	public void closeTemplateSource(Object templateSource) throws IOException {
	}

 

 FreeMarkerConfigurationFactory:

protected TemplateLoader getTemplateLoaderForPath(String templateLoaderPath) {
		if (isPreferFileSystemAccess()) {
			// Try to load via the file system, fall back to SpringTemplateLoader
			// (for hot detection of template changes, if possible).
			try {
				Resource path = getResourceLoader().getResource(templateLoaderPath);
				File file = path.getFile();  // will fail if not resolvable in the file system
				if (logger.isDebugEnabled()) {
					logger.debug(
							"Template loader path [" + path + "] resolved to file path [" + file.getAbsolutePath() + "]");
				}
				return new FileTemplateLoader(file);
			}
			catch (IOException ex) {
				if (logger.isDebugEnabled()) {
					logger.debug("Cannot resolve template loader path [" + templateLoaderPath +
							"] to [java.io.File]: using SpringTemplateLoader as fallback", ex);
				}
				return new SpringTemplateLoader(getResourceLoader(), templateLoaderPath);
			}
		}
		else {
			// Always load via SpringTemplateLoader (without hot detection of template changes).
			logger.debug("File system access not preferred: using SpringTemplateLoader");
			return new SpringTemplateLoader(getResourceLoader(), templateLoaderPath);
		}
	}

 

FreeMarkerConfigurationFactory:

public Configuration createConfiguration() throws IOException, TemplateException {
		Configuration config = newConfiguration();
		Properties props = new Properties();

		// Load config file if specified.
		if (this.configLocation != null) {
			if (logger.isInfoEnabled()) {
				logger.info("Loading FreeMarker configuration from " + this.configLocation);
			}
			PropertiesLoaderUtils.fillProperties(props, this.configLocation);
		}

		// Merge local properties if specified.
		if (this.freemarkerSettings != null) {
			props.putAll(this.freemarkerSettings);
		}

		// FreeMarker will only accept known keys in its setSettings and
		// setAllSharedVariables methods.
		if (!props.isEmpty()) {
			config.setSettings(props);
		}

		if (!CollectionUtils.isEmpty(this.freemarkerVariables)) {
			config.setAllSharedVariables(new SimpleHash(this.freemarkerVariables, config.getObjectWrapper()));
		}

		if (this.defaultEncoding != null) {
			config.setDefaultEncoding(this.defaultEncoding);
		}

		// Register template loaders that are supposed to kick in early.
		if (this.preTemplateLoaders != null) {
			this.templateLoaders.addAll(this.preTemplateLoaders);
		}

		// Register default template loaders.
		if (this.templateLoaderPaths != null) {
			for (String path : this.templateLoaderPaths) {
				this.templateLoaders.add(getTemplateLoaderForPath(path));
			}
		}
		postProcessTemplateLoaders(this.templateLoaders);

		// Register template loaders that are supposed to kick in late.
		if (this.postTemplateLoaders != null) {
			this.templateLoaders.addAll(this.postTemplateLoaders);
		}

		TemplateLoader loader = getAggregateTemplateLoader(this.templateLoaders);
		if (loader != null) {
			config.setTemplateLoader(loader);
		}

		postProcessConfiguration(config);
		return config;
	}

 

分享到:
评论

相关推荐

    FreeMarkder中文手册

    ### FreeMarker中文手册知识点概述 #### 一、FreeMarker简介 FreeMarker是一个用Java语言编写的模板引擎,它基于一种简单的模板语言来处理文本输出。该手册为FreeMarker 2.3.19版本的中文翻译版,由南磊翻译,并...

    freemarker 中文乱码解决

    <bean id="freemakerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <value>/WEB-INF/web/ <prop key="defaultEncoding">gbk ``` #### 方法二:处理...

    freemarkerdebug

    freemarker调试实现了freemarker / spring mvc反模式。 用于跟踪工具开发的代码。 只需使用Maven运行它: mvn jetty:run 打开浏览器: 进行慢速演示用于模板崩溃演示(仅用于获取堆栈跟踪) ......

    freemarker导出Excel、Word、HTMLdemo

    在Java中,Freemarker与ModelAndView、Velocity等模板引擎类似,它通过模板语言(Template Language)来描述数据如何被渲染。... 1. **导出Excel** - 使用Freemarker导出Excel通常涉及到Apache POI库。...

    实训商业源码-支付系统-毕业设计.zip

    实训商业源码-支付系统-毕业设计.zip

    校园失物招领小程序源码可作毕业设计.zip

    校园失物招领微信小程序源码, 失物招领小程序主要为解决大学生时常丢失物品而且很难找回以及归还过程繁琐不方便的问题, 与传统的失物招领方式不同,该款校园失误招领小程序拥有快捷发布寻物启事和失误找领功能, 快速查找、极速归还、高效沟通、防误领冒领等功能, 在开发校园失物招领小程序前与用户访谈发现有近40的同学校园内频繁丢失物品、证件、校园卡等, 数码产品、日用品等,丢失区域主要发生在教学楼、图书馆和食堂。 拾领校园失物招领小程序继承了寻物启事和失物招领,丢失物品或拾取物品都可发布帖子, 首页的横幅滚动公告展示通知公告等,banner图片化的方式更具有视觉吸引力, 最新信息可显示最近发布的招领信息或寻物信息,更加方便快捷的展示信息, 用户可通过首页的发布按钮发布帖子,发布者只需填写物品的相关信息,类别、地点等相关信息, 并且可以填写手机号开启认领验证,并可以一键生成二维码分享或分享至群聊和朋友圈。 列表内可以筛选物品类别或精确搜索,物品详情里可展示物品的相关信息, 确认是自己的物品后可点击认领,然后验证信息,需填写物品的关键信息以作辨认, 防止冒领误领,物品详情页可生成二维码海报分享,还有即时的消息联系功能以提高沟通效率, 发布者还可选择放置在代收处,双方还可以通过拨打电话紧急联系,用于紧急情况,让失物找到主人, 个人中心可以管理发布的物品帖子,管理个人信息,包括昵称、默认学校、手机号的修改、 编辑发布的物品帖子、获取帮助等。帮助用户流畅的使用该小程序。

    实训商业源码-释怀全网首发召唤神龙源码-毕业设计.zip

    实训商业源码-释怀全网首发召唤神龙源码-毕业设计.zip

    实训商业源码-五金器件类企业网站源码-毕业设计.zip

    实训商业源码-五金器件类企业网站源码-毕业设计.zip

    实训商业源码-日主题子主题-毕业设计.zip

    实训商业源码-日主题子主题-毕业设计.zip

    放大镜与遮罩层代码实现与思路

    放大镜与遮罩层代码实现与思路

    毕业论文-TP框架支付-整站商业源码.zip

    毕业论文-TP框架支付-整站商业源码.zip

    Feature not supported in this compiler version - 当前版本不支持某些功能(解决办法).md

    Feature not supported in this compiler version - 当前版本不支持某些功能(解决办法).md

    毕业论文-红包拓客生意宝V2.2.0 解密开源版-整站商业源码.zip

    毕业论文-红包拓客生意宝V2.2.0 解密开源版-整站商业源码.zip

    ANSYS EKM,研发仿真数据管理与流程管理方案.pdf

    ANSYS EKM,研发仿真数据管理与流程管理方案.pdf

    实训商业源码-赞和圈圈-毕业设计.zip

    实训商业源码-赞和圈圈-毕业设计.zip

    ANSYS塑料橡胶行业解决方案介绍.pdf

    ANSYS塑料橡胶行业解决方案介绍.pdf

    2000-2022年上市公司客户、供应商集中度数据

    2000-2022年上市公司客户、供应商集中度数据 1、时间:2000-2022年 2、来源:上市公司NB 3、指标:年份、股票代码、股票简称、行业代码、省份、城市、省份代码、城市代码、上市状态、前五名客户产生的营业收入_亿元、占全年营业收入的比例、前五名供应商产生的采购额_亿元、占全年采购总额的比例、供应链集中度 4、范围:A股上市公司 5、样本量:5.2W+ 6、方法说明:以客户集中度和供应商集中度之和衡量企业供应链集中度,其中客户集中度以前五名客户产生的营业收入占比衡量,供应商集中度以前五名供应商产生的采购额占比衡量 7、参考文献:吴安兵,龚星宇,陈创练等.非金融企业影子银行化的风险承担效应:内在机制与经验证据 邱保印,余梦,左静静.社会信用体系建设能否促进企业数字化转型?——基于社会信用体系改革试点的准自然实验

    jquery-2.0.3.min.js(jQuery下载)

    jquery-2.0.3.min.js(jQuery下载)

    safeint/////

    safeint/////

    实训商业源码-在线全网音乐搜索下载源码-毕业设计.zip

    实训商业源码-在线全网音乐搜索下载源码-毕业设计.zip

Global site tag (gtag.js) - Google Analytics