freemarker页面中调用java方法
2017-03-30 本文已影响262人
H_Man
首先,经过本人的测试,freemarker只能调用Java的Public static 方法及属性
背景说明:框架使用的是spring+springmvc+freemarker
package com.zxsd.test.util;
import java.util.HashMap;
import java.util.Map;
public class UrlUtil {
private static final Map<String, String> urls = new HashMap<String, String>();
static {
urls.put("Nwenzhang.css", "/css/Nwenzhang.css");
urls.put("Guang.css", "/css/Guang.css");
urls.put("bootstrap.min.css", "/bootstrap/css/bootstrap.min.css");
urls.put("Nwenzhang.css", "/css/Nwenzhang.css");
}
public static String getUrl(String key) {
return urls.get(key);
}
}
第一种:
直接将类的实例通过ModelMap传递到前端
Controller中的代码:
</pre><pre name="code" class="java">map.put("urlUtil", new UrlUtil());
HTML页面中的代码:
<#assign name=urlUtl.getUrl("Nwenzhang.css")>${name}
第二种:
定义BaseController 如下:
package com.zxsd.test.action.base;
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 ,需要返回静态类时候 如下:
map.put("urlUtil", useStaticPacker("com.zxsd.test.util.UrlUtil"));
在页面中调用:
${UrlUtil.getUrl("Nwenzhang.css")!}
第三种:
定义FreemarkerStaticModels.java 如下:
package com.zxsd.test.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
UrlUtil=com.zxsd.test.util.UrlUtil
在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.zxsd.test.util.FreemarkerStaticModels" factory-method="getInstance">
<property name="staticModels" ref="freemarkerStatic"/>
</bean>
<!-- freemarker视图解析器 -->
<bean id="freemarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="prefix" value="/page/" />
<property name="suffix" value=".html" />
<property name="contentType" value="text/html; charset=UTF-8" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
<!-- freemarker 静态类配置,在原有<span style="line-height: 25.2px; font-family: verdana, Arial, Helvetica, sans-serif;">freemarker视图解析器</span>配置中加入下一行即可 -->
<span style="white-space:pre"> </span><property name="attributesMap" ref="freemarkerStaticModels"/>
</bean>
最后说下三种的选择:
第一种后端最简便,什么支持都不用,但是前端比较麻烦。
第一种和第二种是用哪些类就加入哪些类。
第三种是直接返回所有配置的类到页面。