在项目中加入freemarker

在spring的配置文件中加入:

<!-- Freemarker配置 -->  
    <!-- <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> jar包中的类 -->
    <!-- 下面是自定义扩展类,为了在freemarker中使用shiro标签 -->
    <bean id="freemarkerConfig" class="com.utils.freemarker.ShiroTagFreeMarkerConfigurer">  
        <property name="templateLoaderPath" value="/WEB-INF/ftl/" /> 
        <property name="freemarkerVariables">    
            <map>    
                <entry key="xml_escape" value-ref="fmXmlEscape" />    
            </map>    
        </property> 
        <property name="defaultEncoding">    
            <value>utf-8</value>    
        </property>    
        <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="classic_compatible">true</prop>  
                <prop key="template_exception_handler">ignore</prop>  
                <!-- 自动导入的模板 -->
                <prop key="auto_import">
                    /common/common.ftl as _com
                </prop>
                <prop key="template_update_delay">5</prop> <!--检查模板是否更新  秒 -->   
            </props> 
        </property> 
    </bean>  

配置视图解析器:

<!--视图解释器 ; MyFreeMarkerView为自定义全局路径 -->  
    <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
        <property name="suffix">  
            <value>.ftl</value>  
        </property>  
        <property name="contentType" value="text/html;charset=UTF-8"></property>
        <property name="viewClass" value="com.utils.model.MyFreeMarkerView" />
    </bean>

自定义类ShiroTagFreeMarkerConfigurer:

package com.utils.freemarker;

import java.io.IOException;

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

import com.jagregory.shiro.freemarker.ShiroTags;

import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModelException;
/**
 * 自定义一个ShiroTagFreeMarkerConfigurer继承Spring本身提供的FreeMarkerConfigurer,
 * 目的是在FreeMarker的Configuration中添加shiro的配置 
 * @author mac
 *
 */
public class ShiroTagFreeMarkerConfigurer extends FreeMarkerConfigurer {
    @Override  
    public void afterPropertiesSet() throws IOException, TemplateException {  
        super.afterPropertiesSet();
        Configuration cfg = this.getConfiguration();
        putInitShared(cfg);
    }  

    public static void put(Configuration cfg,String k,Object v) throws TemplateModelException{

        cfg.setSharedVariable(k,v); 
        cfg.setNumberFormat("#");//防止页面输出数字,变成2,000
    }
    public static void putInitShared(Configuration cfg) throws TemplateModelException{
        //shiro tag 
        put(cfg,"shiro", new ShiroTags()); 
    }
}

自定义类MyFreeMarkerView:
这样在.ftl文件中, 就可以引用 basePath {_v}获取固定值

package com.utils.model;

import java.util.Calendar;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

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

/**
 * 添加freemarker绝对路径.
 * @author mac
 *
 */
public class MyFreeMarkerView extends FreeMarkerView{

    private static final String CONTEXT_PATH = "basePath"; 
    private static final String VERSION = "_v"; 
    /**
     * 重写该方法生成
     */
    @Override
    protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws Exception {
        //服务器绝对路径
        model.put(CONTEXT_PATH, request.getContextPath());
        model.put(VERSION, Calendar.getInstance().getTime().toString());
        super.exposeHelpers(model, request);
    }

    public static void main(String[] args) {
        String time = Calendar.getInstance().getTime().toString();
        System.out.println(time);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值