定義BaseController 以下:html
package com.ly.jxc.controller; import org.springframework.stereotype.Controller; import freemarker.ext.beans.BeansWrapper; import freemarker.template.TemplateHashModel; import freemarker.template.TemplateModelException; @Controller public class BaseController { private final static BeansWrapper wrapper = BeansWrapper .getDefaultInstance(); private final static TemplateHashModel staticModels = wrapper .getStaticModels(); protected static TemplateHashModel useStaticPacker(String packname) { TemplateHashModel fileStatics = null; try { fileStatics = (TemplateHashModel) staticModels.get(packname); } catch (TemplateModelException e) { e.printStackTrace(); } return fileStatics; }; }
其餘controller 繼承 BaseController ,須要返回靜態類時候 以下:java
//register 靜態訪問 map.put("rule",useStaticPacker("com.ly.jxc.RuleUtils"));
頁面使用:web
<#if user.canRead(RuleUtils.BUY_ORDER) || user.canRead(RuleUtils.BUY_LOG) > ..... </#if>
定義FreemarkerStaticModels.java 以下:spring
package com.ly.jxc.util; import java.util.HashMap; import java.util.Properties; import java.util.Set; import freemarker.ext.beans.BeansWrapper; import freemarker.template.TemplateHashModel; /** * 處理freeMarker 訪問靜態類靜態方法 * @author Administrator * */ public class FreemarkerStaticModels extends HashMap<Object, Object>{ /** * */ private static final long serialVersionUID = 1L; private static FreemarkerStaticModels FREEMARKER_STATIC_MODELS; private Properties staticModels; private FreemarkerStaticModels(){ } public static FreemarkerStaticModels getInstance(){ if(FREEMARKER_STATIC_MODELS==null){ FREEMARKER_STATIC_MODELS=new FreemarkerStaticModels(); } return FREEMARKER_STATIC_MODELS; } public Properties getStaticModels() { return staticModels; } public void setStaticModels(Properties staticModels) { if(this.staticModels==null&&staticModels!=null){ this.staticModels = staticModels; Set<String> keys=this.staticModels.stringPropertyNames(); for (String key : keys) { FREEMARKER_STATIC_MODELS.put(key, useStaticPackage(this.staticModels.getProperty(key))); } } } public static TemplateHashModel useStaticPackage(String packageName){ try { BeansWrapper wrapper = BeansWrapper.getDefaultInstance(); TemplateHashModel staticModels = wrapper.getStaticModels(); TemplateHashModel fileStatics = (TemplateHashModel) staticModels.get(packageName); return fileStatics; } catch (Exception e) { e.printStackTrace(); } return null; } }
定義staticClass.properties mvc
RuleUtils=com.ly.jxc.util.RuleUtils
在springmvc.xml 配置文件中app
<!-- freemarker 靜態類的配置位置 --> <bean id="freemarkerStatic" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="classpath:staticClass.properties" /> </bean> <!-- freemarker 靜態類的解析配置--> <bean id="freemarkerStaticModels" class="com.ly.jxc.util.FreemarkerStaticModels" factory-method="getInstance"> <property name="staticModels" ref="freemarkerStatic"/> </bean>
<!-- 配置freeMarker視圖解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="viewClass" value="com.ly.jxc.util.FreeMarkerPath" /> <!-- <property name="viewNames" value="*.ftl"/> --> <property name="contentType" value="text/html; charset=utf-8" /> <property name="allowSessionOverride" value="true" /> <property name="cache" value="true" /> <property name="prefix" value="" /> <property name="suffix" value=".html" /> <property name="order" value="0" /> <!-- freemarker 靜態類配置 --> <property name="attributesMap" ref="freemarkerStaticModels"/>
</bean>
頁面使用 同上。ide
最後說下兩種的選擇: 第一種實用於哪裏用到哪裏調用,第二種是直接返回全部到頁面。this