`

freemarker 根据模板生成内容

 
阅读更多

package com.froad.openapi.common.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.froad.openapi.common.FreeMarkerUtil;
import com.froad.openapi.util.ExceptionMsgFormat;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class FreeMarkerUtilImpl implements FreeMarkerUtil{
   
    final Log logger = LogFactory.getLog(FreeMarkerUtilImpl.class);
    private Map<String, String> templatePathMap ;//= new HashMap<String, String>();
    private Map<String, Template> templateMap = new HashMap<String, Template>();

   
    public FreeMarkerUtilImpl() {
        //initTemplateMap();
    }
   
    public void initTemplateMap() {
        if(null != templatePathMap && templatePathMap.size() > 0) {
            for (String key : templatePathMap.keySet()) {
                try {
                    templateMap.put(key, freeMarkerConfigurer.getConfiguration().getTemplate(templatePathMap.get(key)));
                } catch (Exception e) {
                    logger.error(ExceptionMsgFormat.errorMessageFormat(e));
                }
            }
           
        }
    }

    public Map<String, String> getTemplatePathMap() {
        return templatePathMap;
    }

    public void setTemplatePathMap(Map<String, String> templatePathMap) {
        this.templatePathMap = templatePathMap;
    }

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Autowired
    private Configuration freeMarkerConfiguration;
   
    private List<String> templatePath = new ArrayList<String>();

    public void setFreeMarkerConfigurer(
            FreeMarkerConfigurer freeMarkerConfigurer) {
        this.freeMarkerConfigurer = freeMarkerConfigurer;
    }

    public void setFreeMarkerConfiguration(Configuration freeMarkerConfiguration) {
        this.freeMarkerConfiguration = freeMarkerConfiguration;
    }

    public String getContent(String templateName, Map<String, Object> model) {
        try {
            Template t = this.templateMap.get(templateName);
            return FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ExceptionMsgFormat.errorMessageFormat(ex));
            try {
                Template t = this.templateMap.get(templateName);
                return FreeMarkerTemplateUtils.processTemplateIntoString(t,
                        model);
            } catch (Exception e) {
                e.printStackTrace();
                logger.error(ExceptionMsgFormat.errorMessageFormat(ex));
            }
        }

        return null;
    }
}

配置文件

<bean id="freeMarkerUtil" class=“openapi.common.impl.FreeMarkerUtilImpl" init-method="initTemplateMap">
        <property name="freeMarkerConfigurer" ref="freeMarkerConfigurer" />
        <property name="freeMarkerConfiguration" ref="freeMarkerConfiguration" />
        <property name="templatePathMap" ref="templatePathMap"></property>
    </bean>
   
    <bean id="templatePathMap" class="java.util.HashMap">
        <constructor-arg>
            <map>
                <entry key="Android_SD_Fail.page">
                <value>Android_SD_Fail.page</value>
                </entry>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics