动机
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-framework</groupId>
<artifactId>rapid-core</artifactId>
<version>4.0.5</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</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>
<!--设置freemarker环境属性-->
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">5</prop><!--刷新模板的周期,单位为秒 -->
<prop key="default_encoding">UTF-8</prop><!--模板的编码格式 -->
<prop key="locale">UTF-8</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="whitespace_stripping">true</prop>
<prop key="tag_syntax">auto_detect</prop>
<prop key="url_escaping_charset">UTF-8</prop>
<prop key="classic_compatible">true</prop>
</props>
</property>
<!--设置模板继承的参数-->
<property name="freemarkerVariables">
<map>
<!--<entry key="xml_escape" value-ref="fmXmlEscape" />-->
<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>
<!-- freemaker Directive-->
<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="xml_escape" value-ref="fmXmlEscape" />-->
<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的设置
<!-- freemaker Directive-->
<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