項目中使用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">