FreeMarker模板继承

动机

freemarker默认是没有模板继承功能的,为其添加新指令@extends,@block,@override,@super,这些指令属于rapid-framework的扩展。

问题

但是,都说加上rapid-framework的jar包,然后修改配置文件就够了, 但是你会发现出现一下问题:

12-May-2017 16:43:11.123 SEVERE [http-nio-8081-exec-1]
org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service()
for servlet [mvc-dispatcher] in context with path [] threw exception
[Handler processing failed; nested exception is
java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils]
with root cause java.lang.ClassNotFoundException:
org.apache.commons.lang.StringUtils at
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
at
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
at
cn.org.rapid_framework.freemarker.directive.DirectiveUtils.getRequiredParam(DirectiveUtils.java:34)
at
cn.org.rapid_framework.freemarker.directive.BlockDirective.execute(BlockDirective.java:22)
at freemarker.core.Environment.visit(Environment.java:427) at
freemarker.core.UnifiedCall.accept(UnifiedCall.java:101) at
freemarker.core.Environment.visit(Environment.java:327) at
freemarker.core.Environment.visit(Environment.java:333) at
freemarker.core.Environment.process(Environment.java:306) at
freemarker.template.Template.process(Template.java:384) …… at
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

注意, 最开头的报错:

java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils]
with root cause java.lang.ClassNotFoundException:
org.apache.commons.lang.StringUtils

所以还需要添加commons.lang相关jar包

开发环境

Tomcat 8.0.42
java 1.8.0_121
IntelliJ IDEA

需要的jar包

  • maven用户
    在pom.xml中添加以下依赖
<dependency>
     <groupId>com.googlecode.rapid-frameworkgroupId>
     <artifactId>rapid-coreartifactId>
     <version>4.0.5version>
dependency>
<dependency>
     <groupId>commons-langgroupId>
     <artifactId>commons-langartifactId>
     <version>2.6version>
dependency>
  • 直接下载jar包
    freemarker默认是没有模板继承功能的,为其添加新指令@extends,@block,@override,@super,这些指令属于rapid-framework的扩展
    下载地址: http://www.java2s.com/Code/JarDownload/rapid/rapid-core-4.0.5.jar.zip
    另外还需commons-lang.jar的支持
    下载地址: https://mirrors.tuna.tsinghua.edu.cn/apache//commons/lang/binaries/commons-lang-2.6-bin.zip

配置文件

需要在${servelet-name}.xml中添加Freemarker拓展配置


    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath">
            <value>WEB-INF/FTL/value>
        property>
        
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">5prop>
                <prop key="default_encoding">UTF-8prop>
                <prop key="locale">UTF-8prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ssprop>
                <prop key="time_format">HH:mm:ssprop>
                <prop key="number_format">0.####prop>
                <prop key="boolean_format">true,falseprop>
                <prop key="whitespace_stripping">trueprop>
                <prop key="tag_syntax">auto_detectprop>
                <prop key="url_escaping_charset">UTF-8prop>
                <prop key="classic_compatible">trueprop>
            props>
        property>
        
        <property name="freemarkerVariables">
            <map>
                
                <entry key="extends" value-ref="extendsDirective"/>
                <entry key="override" value-ref="overrideDirective"/>
                <entry key="block" value-ref="blockDirective"/>
                <entry key="super" value-ref="superDirective"/>
            map>
        property>
    bean>

    
    <bean id="blockDirective" class="cn.org.rapid_framework.freemarker.directive.BlockDirective"/>
    <bean id="extendsDirective" class="cn.org.rapid_framework.freemarker.directive.ExtendsDirective"/>
    <bean id="overrideDirective" class="cn.org.rapid_framework.freemarker.directive.OverrideDirective"/>
    <bean id="superDirective" class="cn.org.rapid_framework.freemarker.directive.SuperDirective"/>

添加了

property name="freemarkerVariables">
            <map>
                
                <entry key="extends" value-ref="extendsDirective"/>
                <entry key="override" value-ref="overrideDirective"/>
                <entry key="block" value-ref="blockDirective"/>
                <entry key="super" value-ref="superDirective"/>
            map>
        property>

与之相对应的是bean的设置


    <bean id="blockDirective" class="cn.org.rapid_framework.freemarker.directive.BlockDirective"/>
    <bean id="extendsDirective" class="cn.org.rapid_framework.freemarker.directive.ExtendsDirective"/>
    <bean id="overrideDirective" class="cn.org.rapid_framework.freemarker.directive.OverrideDirective"/>
    <bean id="superDirective" class="cn.org.rapid_framework.freemarker.directive.SuperDirective"/>

至于@block,@extends, @override的实例,可以参考链接:
http://coolpeanut.site/home1/page4.html

你可能感兴趣的:(FreeMarker模板继承)