nutz+jquery+Velocity开发的一款影视cms

programming 2013-01-25
nutz+jquery开发的一款影视cms
TemplateEngine 渲染模板

渲染接口
package com.hzsuwang.hzqvod.tags.core;

import java.util.Map;
public interface TemplateEngine {

	public String render(String template, Map<String, Object> params) throws Exception;


	public String render(String template, String key, Object value) throws Exception;
}


Velocity渲染实现
package com.hzsuwang.hzqvod.tags.core.impl;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.runtime.log.LogSystem;
import org.nutz.ioc.loader.annotation.IocBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hzsuwang.hzqvod.tags.core.TemplateEngine;
import com.hzsuwang.hzqvod.util.StringUtil;
@SuppressWarnings("deprecation")
@IocBean(create = "init")
public class TemplateEngineVelocityImpl implements TemplateEngine {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	public void init() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("TemplateEngineVelocityImpl init......");
		}
		ExtendedProperties configuration = new ExtendedProperties();
		configuration.setProperty(Velocity.VM_LIBRARY, "");
		configuration.setProperty(Velocity.RESOURCE_MANAGER_LOGWHENFOUND, "false");
		configuration.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
		configuration.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
		configuration.setProperty(Velocity.PARSER_POOL_SIZE, 50);
		configuration.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new LogSystem() {
			public void init(RuntimeServices runtimeServices) {
			}

			public void logVelocityMessage(int level, String message) {
				switch (level) {
				case DEBUG_ID:
					logger.debug(message);
					break;

				case INFO_ID:
					logger.info(message);
					break;

				case WARN_ID:
					logger.warn(message);
					break;

				case ERROR_ID:
					logger.error(message);
					break;
				}
			}
		});
		Velocity.setExtendedProperties(configuration);
		Velocity.init();
	}

	@Override
	public String render(String template, Map<String, Object> params) throws Exception {
		if (StringUtil.isEmpty(template)) {
			return "";
		}
		VelocityContext context = new VelocityContext(params);
		StringWriter writer = new StringWriter();
		Velocity.evaluate(context, writer, this.getClass().getName(), template);
		return writer.toString();
	}

	@Override
	public String render(String template, String key, Object value) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(key, value);
		return render(template, params);
	}

}





Global site tag (gtag.js) - Google Analytics