项目中Spring结合Freemaker渲染网页
前言
项目中使用spring+freemaker来渲染web,发现freemake在页面框架设置,常亮配置,自定义指令方法均比较方便,所以整理了一下。其实项目中还是用了sitemesh,这个还没有仔细研究。在看项目代码的时候,发现页面有一段语句<@sp.static/>,不知道是什么,深入查找后,才知道是freemaker的自定义指令。
第一步
在spring中配置freemaker,主要是配置了模板目录和导入spring.ftl
<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文件
<#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中配置视图解析器
<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文件中使用了
<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"> 相关推荐
81314797 2020-11-18
89314493 2020-11-03
81941231 2020-09-17
thisisid 2020-09-09
如狼 2020-08-15
82384399 2020-06-16
86384798 2020-05-12
80183053 2020-05-02
86384798 2020-04-26
86384798 2020-04-11
rionchen 2020-04-09
86384798 2020-04-07
86384798 2020-04-04
80183053 2020-03-07
87201943 2020-03-06
83961233 2020-02-26
87201943 2020-02-21