项目中使用spring+freemaker来渲染web,发现freemake在页面框架设置,常亮配置,自定义指令方法均比较方便,因此整理了一下。其实项目中仍是用了sitemesh,这个尚未仔细研究。在看项目代码的时候,发现页面有一段语句<@sp.static/>,不知道是什么,深刻查找后,才知道是freemaker的自定义指令。
在spring中配置freemaker,主要是配置了模板目录和导入spring.ftljavascript
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath"> <value>/html/</value> </property> <property name="freemarkerSettings"> <props> <prop key="locale">zh_CN</prop> <prop key="defaultEncoding">UTF-8</prop> <prop key="date_format">yyyy-MM-dd</prop> <prop key="time_format">HH:mm:ss</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="number_format">0.################</prop> <prop key="boolean_format">true,false</prop> <prop key="auto_import">"/spring.ftl" as sp</prop> </props> </property> </bean>
spring.ftl是主要的freemaker宏文件,同时也导入了其余的ftl文件html
<#macro closeTag> <#if xhtmlCompliant?exists && xhtmlCompliant>/><#else>></#if> </#macro> <#macro static>/static</#macro> <#macro seo>http://usolvpay.ttf.com/homesite</#macro> <#macro contextPath>${rc.contextPath}</#macro> <#include "common/macro/header.ftl"> <#include "common/macro/header_app.ftl"> <#include "common/macro/header_corp.ftl"> <#include "common/macro/bill.ftl">
spring中配置视图解析器java
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="requestContextAttribute" value="rc" /> <property name="cache"> <value>true</value> </property> <property name="suffix"> <value>.html</value> </property> <property name="exposeSpringMacroHelpers" value="true" /> <property name="contentType"> <value>text/html; charset=UTF-8</value> </property> </bean>
如今就能够在html文件中使用了jquery
<script src="<@sp.static/>/js/pay/common/jquery-1.3.2.js" type="text/javascript"></script> <form id= "sumpaysubmit" name="sumpaysubmit" action="${cashierUrl}" method="post">