一.利用BaseController 方式处理
定义BaseController 如下:
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 ,需要返回静态类时候 如下:
//register 静态访问 map.put("rule",useStaticPacker("com.ly.jxc.RuleUtils"));
页面使用:
<#if user.canRead(RuleUtils.BUY_ORDER) || user.canRead(RuleUtils.BUY_LOG) > ..... </#if>
二 使用配置方式
定义FreemarkerStaticModels.java 如下:
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
RuleUtils=com.ly.jxc.util.RuleUtils
在springmvc.xml 配置文件中
<!-- 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>
页面使用 同上。
最后说下两种的选择: 第一种实用于哪里用到哪里调用,第二种是直接返回所有到页面。
时间: 2024-11-02 18:25:01