Freemarker之Spring篇
Freemarker之Spring篇
溪水涛 发表于2年前
Freemarker之Spring篇
  • 发表于 2年前
  • 阅读 16
  • 收藏 0
  • 点赞 1
  • 评论 0

【腾讯云】新注册用户域名抢购1元起>>>   

  1. 目录结构

2. maven 依赖包 pom.xml

<dependencies>
    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.19</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
    </dependency>
</dependencies>

3.  spring 加载Classpath或jar中的模版

SpringTemplateLoaderFactoryBean.java

public class SpringTemplateLoaderFactoryBean implements FactoryBean<SpringTemplateLoader>,InitializingBean {

    protected SpringTemplateLoader springTemplateLoader;

    private String locations;

    @Override
    public SpringTemplateLoader getObject() throws Exception {
        return springTemplateLoader;
    }

    @Override
    public Class<?> getObjectType() {
        return SpringTemplateLoader.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if(springTemplateLoader == null)  springTemplateLoader = new SpringTemplateLoader(new DefaultResourceLoader(),locations);
    }

    public void setLocations(String locations) {
        this.locations = locations;
    }
}

4.  spring集成freemarker的配置 spring-context-freemarker.xml

    

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

       <context:component-scan base-package="com.nexus.spider.freemarker" use-default-filters="false">
              <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
              <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
       </context:component-scan>

       <bean id="freemarkerConfigFactoryBean" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
              <property name="defaultEncoding" value="UTF-8" />
              <property name="freemarkerSettings">
                     <props>
                            <prop key="template_update_delay">5</prop><!--刷新模板的周期,单位为秒 -->
                            <prop key="default_encoding">UTF-8</prop><!--模板的编码格式 -->
                            <prop key="locale">zh_CN</prop><!-- 本地化设置 -->
                            <prop key="date_format">yyyy-MM-dd</prop>
                            <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                            <prop key="time_format">HH:mm:ss</prop>
                            <prop key="number_format">0.####</prop>
                            <prop key="boolean_format">true,false</prop>
                            <prop key="classic_compatible">true</prop>
                            <prop key="whitespace_stripping">true</prop>
                            <prop key="tag_syntax">auto_detect</prop>
                            <prop key="url_escaping_charset">UTF-8</prop>
                            <prop key="template_exception_handler">ignore</prop>
                     </props>
              </property>
              <property name="preTemplateLoaders">
                     <array value-type="freemarker.cache.TemplateLoader">
                            <ref bean="dynamicTemplateLoader"/>
                     </array>
              </property>
       </bean>

       <bean id="dynamicTemplateLoader" class="com.nexus.spider.freemarker.spring.SpringTemplateLoaderFactoryBean">
              <property name="locations" value="com/nexus/spider/gen/template"/>
       </bean>

</beans>

5. freemarker 的简单封装

    

FreemarkerService.java

@Component
public class FreemarkerService {

    @Resource
    private Configuration configuration;

    public String renderToString(String name,Map<String,Object> params) throws IOException, TemplateException {
        StringWriter stringWriter = new StringWriter();
        render(name,params,stringWriter);
        return stringWriter.toString();

    }

    public void render(String name,Map<String,Object> params,Writer out) throws IOException, TemplateException {
        Template template = configuration.getTemplate(name);
        template.process(params,out);
        out.flush();
    }

    public void render(String name,Map<String,Object> params,String outPath) throws IOException, TemplateException {
        File file = new File(outPath);
        if(!file.exists()) file.mkdirs();
        FileWriter fileWriter = new FileWriter(file);
        try {
            render(name,params,fileWriter);
        } finally {
            fileWriter.close();
        }
    }
}

6.编写测试用例

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 3
码字总数 1929
×
溪水涛
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: