在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);
}
}