Free Marker의 정거장 방식

5574 단어
자세히 보기
FreeMarker의 귀속 생성 사이트 클래스

package com.uuzz.demo.template.service.impl;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.uuzz.demo.template.service.HtmlCreator;
import com.uuzz.demo.template.util.FreeMarderFactory;

import freemarker.cache.StringTemplateLoader;
import freemarker.core.TemplateElement;
import freemarker.ext.beans.StringModel;
import freemarker.template.Template;
import freemarker.template.TemplateSequenceModel;

@Service
public class HtmlCreatorImpl implements HtmlCreator {

	private String homeTemplate;

	private String basePath;

	@Override
	public String getHomeTemplate() {
		return homeTemplate;
	}

	@Override
	public void setHomeTemplate(String homeTemplate) {
		this.homeTemplate = homeTemplate;
	}

	@Override
	public String getBasePath() {
		return basePath;
	}

	@Override
	public void setBasePath(String basePath) {
		this.basePath = basePath;
	}

	@Override
	public boolean createHtmlByNameAndData(String templateName, Map dataMap) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean createHtmlByTemplateAndData(Map templateMap, Map dataMap) {

		StringTemplateLoader stringTemplateLoader = (StringTemplateLoader) FreeMarderFactory.getTemplateLoader();

		Iterator iterator = templateMap.keySet().iterator();

		while (iterator.hasNext()) {
			String key = iterator.next();
			stringTemplateLoader.putTemplate(key, templateMap.get(key));
		}

		recuriseHtml(getHomeTemplate(), stringTemplateLoader, dataMap);

		FreeMarderFactory.getConfiguration().clearTemplateCache();

		iterator = templateMap.keySet().iterator();
		while (iterator.hasNext()) {
			String key = iterator.next();
			try {
				Template template = FreeMarderFactory.getConfiguration().getTemplate(key);

				File afile = new File(this.getBasePath() + key);

				if (!afile.exists()) {
					afile.createNewFile();
				}

				Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile), "UTF-8"));

				template.process(dataMap, out);
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return false;
	}

	public void recuriseHtml(String templateName, StringTemplateLoader stringTemplateLoader, Map rootMap) {
		try {
			Template template = FreeMarderFactory.getConfiguration().getTemplate(templateName, "UTF-8");

			TemplateSequenceModel list = template.getRootTreeNode().getChildNodes();

			for (int i = 0; i < list.size(); i++) {
				StringModel model = (StringModel) list.get(i);
				TemplateElement element = (TemplateElement) model.getWrappedObject();
				if (element.getNodeName().equals("Include")) {
					String templateSource = element.getSource();
					recuriseHtml(templateSource.substring(templateSource.indexOf("\"") + 2,
							templateSource.lastIndexOf("\"")), stringTemplateLoader, rootMap);
				}
			}

			StringWriter out = new StringWriter();
			Map dataMap = (Map) rootMap.get(templateName);
			template.process(dataMap, out);

			stringTemplateLoader.putTemplate(templateName, out.toString());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


호출 방식

		Map templateMap = new HashMap();
		Map dataMap = new HashMap();
		QueryTestBean t = new QueryTestBean();
// 
		List resultList = testServiceImpl.queryPojoForList(t);

		//  
		for (int i = 0; i < resultList.size(); i++) {
			TestBean testBean = resultList.get(i);

			templateMap.put(testBean.getColumn_2(), testBean.getColumn_6());

			List> templateDataMap = new ArrayList>();
			Map data = new HashMap();

			Map map = new HashMap();
			map.put("name", " !" + testBean.getColumn_2());
			map.put("href", "href");
			templateDataMap.add(map);

			data.put("list", templateDataMap);

			dataMap.put(testBean.getColumn_2(), data);
		}
		
		htmlCreator.setHomeTemplate("index.html");
		htmlCreator.setBasePath(this.request.getServletContext().getRealPath("/html" )+ "/");

		htmlCreator.createHtmlByTemplateAndData(templateMap, dataMap);


좋은 웹페이지 즐겨찾기