Mingyang.net:自定义FreeMarkerView

自定义FreeMarkerView的目的是为了放一些公共的变量到FreeMarker模版里面.
spring-context.xml:

<!-- ********************************************************************** 
     ** FreeMaker解析器                                                      ** 
     ********************************************************************** -->
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <!--<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />-->
    <property name="viewClass" value="net.mingyang.cms.util.FtlView" />   
    <property name="contentType" value="text/html;charset=UTF-8" />
    <property name="exposeRequestAttributes" value="true" />  
    <property name="exposeSessionAttributes" value="true" />  
    <property name="exposeSpringMacroHelpers" value="true" />
    <!-- <property name="suffix" value=".ftl" /> -->  
    <property name="order" value="0" />
</bean>
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> 
    <property name="templateLoaderPath" value="/WEB-INF/views/" /> 
    <property name="freemarkerSettings"> 
        <props> 
            <prop key="template_update_delay">0</prop> 
            <prop key="default_encoding">UTF-8</prop> 
            <prop key="number_format">0.##########</prop> 
            <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
            <prop key="date_format">yyyy-MM-dd</prop> 
            <prop key="classic_compatible">true</prop> 
            <prop key="template_exception_handler">ignore</prop> 
        </props> 
    </property> 
</bean>

FtlView.java:

package net.mingyang.cms.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.view.freemarker.FreeMarkerView;

public class FtlView extends FreeMarkerView
{
    @Override
    protected void exposeHelpers(Map<String, Object> model,
            HttpServletRequest request) throws Exception
    {
        model.put("contextPath", request.getContextPath());
        super.exposeHelpers(model, request);
    }
    
}

Mingyang.net:自定义FreeMarkerView,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。