项目中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">
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值