本节主要分析Spring + FreeMarker的集成。
先介绍一下 spring+freemarker的集成主要涉及以下配置项:
1、配置FreeMarkerConfigurer
通过freemarkerSettings属性,也能够设置其他FreeMarker的配置。例如,FreeMarker默认每隔5秒检查模板是否被更新,如果已经更新了,就会重新加载并分析模板。但经常检查模板是否更新可能比较耗时。如果你的应用运行在生产模式下,而且你预期模板不会经常更新,则可以将更新的延迟时间延长至一个小时或者更久。
可以通过为freemarkerSettings属性设置template_update_delay达到这一目的。例如:
设置template_update_delay为3600(秒),因此FreeMarker只有在1个小时之后才检查模板是否更新。
2、解析FreeMarker视图
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="suffix"><value>.ftl</value></property>
</bean>
将请求和会话属性作为变量暴露给FreeMarker模板使用。要做到这一点,可以设置exposeRequestAttributes或者exposeSessionAttributes为true:
使用这些宏,必须设置FreeMarkerViewResolver的exposeMacroHelpers属性为true:
<property name="exposeSpringMacroHelpers">
<value>true</value>
</property>
3、在FreeMarker中绑定表单域
<@spring.bind "command.phone" />
phone: <input type="text"
name="${spring.status.expression}"
value="${spring.status.value}">
<font color="#FF0000">${spring.status.errorMessage}</font><br>
<@spring.bind "command.email" />
email: <input type="text"
name="${spring.status.expression}"
value="${spring.status.value}">
<font color="#FF0000">${spring.status.errorMessage}</font><br>
4、最后,你还需要做一件事才能使用FreeMarker宏
<@spring.bindEscaped>的FreeMarker模板的顶部增加以下一行: <#import "/spring.ftl" as spring />
这一行会在模板中导入Spring的FreeMarker宏。
Jeecms中相关的配置文件如下----------------->:
总体说明:
1、定义FreeMaker引入文件,index.ftl前缀为p,spring.ftl的前缀为s,这样在饮用ftl模版的宏定义时,可以简略使用。
<prop key="auto_import">/WEB-INF/ftl/pony/index.ftl as p,/WEB-INF/ftl/spring.ftl as s</prop>
在html表单中的使用,例如:
<div class="body-box"> <@p.form id="jvForm" action="o_update.do" labelWidth="12"> <@p.td 。。。。 </div>
2、在jeecms中,请求回话属性都不允许暴露给freemarker使用。。(不知道是否跟freemarker不支持集群有关系????这样是否可以支持集群)
.... <property name="exposeRequestAttributes" value="false"/> <property name="exposeSessionAttributes" value="false"/> ....
以下是涉及到的配置文件:
....
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/application-context.xml
/WEB-INF/config/cache-context.xml
/WEB-INF/config/captcha-context.xml
/WEB-INF/config/jeecore-context.xml
/WEB-INF/config/jeecms-context.xml
</param-value>
</context-param>
....
在这个配置文件中,freeMarkerConfigurer作为 StaticPageSvcImpl的一个属性,注入到这个类中。
freeMarkerConfigurer的配置包括freemarker定义的标签(见freemarkerVariables),freemarkerSettings中定义了一些详细的属性。
详见前面的描述。
<bean id="staticPageSvc" class="com.jeecms.cms.staticpage.StaticPageSvcImpl"> <property name="tplMessageSource" ref="tplMessageSource"/> <property name="freeMarkerConfigurer"> <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="freemarkerVariables"> <map> <entry key="uuid" value-ref="uuid"/> <entry key="process_time" value-ref="process_time"/> <entry key="text_cut" value-ref="text_cut"/> <entry key="html_cut" value-ref="html_cut"/> <entry key="cms_pagination" value-ref="cms_pagination"/> <entry key="cms_channel_list" value-ref="cms_channel_list"/> <entry key="cms_channel_page" value-ref="cms_channel_page"/> <entry key="cms_channel" value-ref="cms_channel"/> <entry key="cms_content" value-ref="cms_content"/> <entry key="cms_content_list" value-ref="cms_content_list"/> <entry key="cms_content_page" value-ref="cms_content_page"/> <entry key="cms_tag_list" value-ref="cms_tag_list"/> <entry key="cms_tag_page" value-ref="cms_tag_page"/> <entry key="cms_topic_list" value-ref="cms_topic_list"/> <entry key="cms_topic_page" value-ref="cms_topic_page"/> <entry key="cms_comment_list" value-ref="cms_comment_list"/> <entry key="cms_comment_page" value-ref="cms_comment_page"/> <entry key="cms_guestbook_ctg_list" value-ref="cms_guestbook_ctg_list"/> <entry key="cms_guestbook_list" value-ref="cms_guestbook_list"/> <entry key="cms_guestbook_page" value-ref="cms_guestbook_page"/> <entry key="cms_vote" value-ref="cms_vote"/> <entry key="cms_friendlink_ctg_list" value-ref="cms_friendlink_ctg_list"/> <entry key="cms_friendlink_list" value-ref="cms_friendlink_list"/> <entry key="cms_lucene_list" value-ref="cms_lucene_list"/> <entry key="cms_lucene_page" value-ref="cms_lucene_page"/> <entry key="cms_advertising" value-ref="cms_advertising"/> </map> </property> <property name="templateLoaderPath" value=""/> <property name="freemarkerSettings"> <props> <prop key="tag_syntax">auto_detect</prop> <prop key="template_update_delay">5</prop> <prop key="defaultEncoding">UTF-8</prop> <prop key="url_escaping_charset">UTF-8</prop> <prop key="locale">zh_CN</prop> <prop key="boolean_format">true,false</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="date_format">yyyy-MM-dd</prop> <prop key="time_format">HH:mm:ss</prop> <prop key="number_format">0.######</prop> <prop key="whitespace_stripping">true</prop> <prop key="auto_import">/WEB-INF/ftl/pony/index.ftl as p,/WEB-INF/ftl/spring.ftl as s</prop> </props> </property> </bean> </property> </bean>
view resolver 配置:
<bean id="freemarkerViewResolver" class="com.jeecms.common.web.springmvc.RichFreeMarkerViewResolver"> <property name="prefix" value="/jeecms_sys/"/> <property name="suffix" value=".html"/> <property name="contentType" value="text/html; charset=UTF-8"/> <property name="exposeRequestAttributes" value="false"/> <property name="exposeSessionAttributes" value="false"/> <property name="exposeSpringMacroHelpers" value="true"/> </bean>
freemarkerConfig 配置:
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF"/>
<property name="freemarkerVariables">
<map>
<!--在FCK编辑器中需要用到appBase,以确定connector路径。-->
<entry key="appBase" value="/jeeadmin/jeecms"/>
<!--后台管理权限控制-->
<entry key="cms_perm" value-ref="cms_perm"/>
<entry key="text_cut" value-ref="text_cut"/>
<entry key="html_cut" value-ref="html_cut"/>
</map>
</property>
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">0</prop>
<prop key="defaultEncoding">UTF-8</prop>
<prop key="url_escaping_charset">UTF-8</prop>
<prop key="locale">zh_CN</prop>
<prop key="boolean_format">true,false</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">0.######</prop>
<prop key="whitespace_stripping">true</prop>
<prop key="auto_import">/ftl/pony/index.ftl as p,/ftl/spring.ftl as s</prop>
</props>
</property>
</bean>
view resolver 配置:
<bean id="freemarkerViewResolver" class="com.jeecms.common.web.springmvc.SimpleFreeMarkerViewResolver">
<property name="contentType" value="text/html; charset=UTF-8"/>
<property name="exposeRequestAttributes" value="false"/>
<property name="exposeSessionAttributes" value="false"/>
<property name="exposeSpringMacroHelpers" value="true"/>
</bean>
freemarkerConfig 配置:参数配置与jeecms-servlet-admin.xml类似。
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="freemarkerVariables">
<map>
<entry key="uuid" value-ref="uuid"/>
<entry key="process_time" value-ref="process_time"/>
<entry key="text_cut" value-ref="text_cut"/>
<entry key="html_cut" value-ref="html_cut"/>
<entry key="cms_pagination" value-ref="cms_pagination"/>
<entry key="cms_channel_list" value-ref="cms_channel_list"/>
<entry key="cms_channel_page" value-ref="cms_channel_page"/>
<entry key="cms_channel" value-ref="cms_channel"/>
<entry key="cms_content" value-ref="cms_content"/>
<entry key="cms_content_list" value-ref="cms_content_list"/>
<entry key="cms_content_page" value-ref="cms_content_page"/>
<entry key="cms_tag_list" value-ref="cms_tag_list"/>
<entry key="cms_tag_page" value-ref="cms_tag_page"/>
<entry key="cms_topic_list" value-ref="cms_topic_list"/>
<entry key="cms_topic_page" value-ref="cms_topic_page"/>
<entry key="cms_comment_list" value-ref="cms_comment_list"/>
<entry key="cms_comment_page" value-ref="cms_comment_page"/>
<entry key="cms_guestbook_ctg_list" value-ref="cms_guestbook_ctg_list"/>
<entry key="cms_guestbook_list" value-ref="cms_guestbook_list"/>
<entry key="cms_guestbook_page" value-ref="cms_guestbook_page"/>
<entry key="cms_vote" value-ref="cms_vote"/>
<entry key="cms_lucene_list" value-ref="cms_lucene_list"/>
<entry key="cms_lucene_page" value-ref="cms_lucene_page"/>
<entry key="cms_friendlink_ctg_list" value-ref="cms_friendlink_ctg_list"/>
<entry key="cms_friendlink_list" value-ref="cms_friendlink_list"/>
<entry key="cms_advertising" value-ref="cms_advertising"/>
</map>
</property>
<!--
<property name="preTemplateLoaders">
<list>
<ref bean="tplMng"/>
</list>
</property>
-->
<property name="templateLoaderPath" value=""/>
<property name="freemarkerSettings">
<props>
<prop key="tag_syntax">auto_detect</prop>
<prop key="template_update_delay">5</prop>
<prop key="defaultEncoding">UTF-8</prop>
<prop key="url_escaping_charset">UTF-8</prop>
<prop key="locale">zh_CN</prop>
<prop key="boolean_format">true,false</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">0.######</prop>
<prop key="whitespace_stripping">true</prop>
<!--空值处理<prop key="classic_compatible">true</prop>-->
<prop key="auto_import">/WEB-INF/ftl/pony/index.ftl as p,/WEB-INF/ftl/spring.ftl as s</prop>
</props>
</property>
</bean>
至此,JeeCms中的FreeMarker的配置已经分析完毕。