• SpringMVC Freemarker 全局变量的三种配置方式


     方法一 直接在spring-servlet.xml 中进行配置

        <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
            <property name="location" value="classpath:freemarker.properties" />  
        </bean>  
        <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
            <property name="templateLoaderPaths" value="/templates" />
            <property name="defaultEncoding" value="utf-8" />
            <property name="freemarkerSettings">
                <props>
                    <prop key="template_update_delay">0</prop>
                    <prop key="url_escaping_charset">UTF-8</prop>
                    <prop key="locale">UTF-8</prop>
                    <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                    <prop key="date_format">yyyy-MM-dd</prop>
                    <prop key="number_format">#.##</prop>
                    <prop key="classic_compatible">true</prop>
                </props>
            </property>
            <property name="freemarkerVariables">
                <map>
                    <entry key="BasePath" value="${base.path}" />
                    <entry key="IncPath" value="${web.root}" />
                    <entry key="xml_escape" value-ref="fmXmlEscape" />
                </map>
            </property>
        </bean>
    
        <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />    

     freemarker.properties

    base.path=localhost:8080
    web.root=www.springmvc.com
     
     
    方法二(推荐)自定义一个视图,继承FreeMarkerView
    public class MyFreeMarkerView extends FreeMarkerView {
        private static final String CONTEXT_PATH = "base";
    
        @Override
        protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws Exception {
            model.put(CONTEXT_PATH, request.getContextPath());
            model.put("cxb", "caoxiaobo");
            super.exposeHelpers(model, request);
        }
    }
        <!--视图解释器 -->  
        <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 
            <property name="exposeRequestAttributes" value="true" />
            <property name="exposeSessionAttributes" value="true" />
         <!-- 自定义视图 -->
         <property name="viewClass"> <value>com.view.freemarker.MyFreeMarkerView</value> </property> <property name="cache" value="true" /> <!-- <property name="prefix" value="/" /> --> <property name="suffix" value=".ftl"/> <property name="contentType" value="text/html;charset=UTF-8"></property> </bean>
     
    方法三 自定义一个 FreeMarkerConfigurer 继承 FreeMarkerConfigurationFactory 并实现FreeMarkerConfig, InitializingBean, ResourceLoaderAware, ServletContextAware
    // 这里其实就是完整的替换了FreeMarkerConfigurer
    public
    class MyFreeMarkerConfigurer extends FreeMarkerConfigurationFactory implements FreeMarkerConfig, InitializingBean, ResourceLoaderAware, ServletContextAware { private Configuration configuration; private TaglibFactory taglibFactory; @Override public void afterPropertiesSet() throws IOException, TemplateException { if (this.configuration == null) { this.configuration = createConfiguration(); } SimpleHash model = new SimpleHash(); model.put("baseUrl", "www.springmvc.com"); this.configuration.setAllSharedVariables(model); } // ... 省略很多方法 }
        <bean id="freemarkerConfig" class="com.view.freemarker.MyFreeMarkerConfigurer"> 
            <property name="templateLoaderPath" value="/" />   
            <property name="freemarkerSettings">  
                <props>  
                    <prop key="locale">zh_CN</prop>
                    <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="classic_compatible">true</prop>  
                    <prop key="template_exception_handler">ignore</prop>  
                </props>  
            </property>
        </bean> 
  • 相关阅读:
    Cordova/Cordova.h file not found的解决方法
    使用MethodSwizzle导致按home app进入后台或者app间切换发生crash的解决方法
    基于iOS上MDM技术相关资料整理及汇总
    iOS集成微信支付
    iOS集成支付宝支付
    最新apple邓白氏码申请地址
    游戏的定价
    许久未更,随便侃侃
    记一个有趣的梦
    由《掟上今日子的备忘录》引发的联想
  • 原文地址:https://www.cnblogs.com/caoxb/p/9375864.html
Copyright © 2020-2023  润新知