参考:
http://grails.org/doc/latest/ref/Tags/select.html
http://freemarker.sourceforge.net/
调用:
<@d_select
id="type_4" optionKey="position" optionValue="id" list=adSpaceBoList callBack="test();" value="4f0eabc9073c1f0de52b25d2" ></@d_select>
指定list对象名:list=adSpaceBoList;选项key对应集合中的属性:optionKey="position";选项value对应集合中的属性:optionValue="id";
实现类:
implements TemplateDirectiveModel
;
public void execute
(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
throws TemplateException, IOException {
// id
String id = MapUtils.getString(params, "id", "selectId");
// multiple
boolean
multiple = Boolean.valueOf(MapUtils.getString(params, "multiple"));
// onEventName:默认有callback时为onChange
String onEventName = MapUtils.getString(params, "onEventName","onChange");
Map<String, Object> templateContext = new HashMap<String, Object>();
//list
Object temp = params.get("list");
if(temp instanceof SimpleSequence){
SimpleSequence lists = (SimpleSequence) params.get("list");
templateContext.put("list",lists.toList());
}
//strList
String strList = MapUtils.getString(params, "strList");// strList : 文字:1,图片:2
if(StringUtils.isNotBlank(strList)){
if(StringUtils.isNotBlank(strList)){
templateContext.put("strList", Arrays.asList(strList.split(",")));
}
}
//map
SimpleHash map=(SimpleHash)params.get("map");
if(null!=map){
templateContext.put("map",map.toMap());
}
templateContext.put("id", id);
//.....
Template template = env.getConfiguration().getTemplate("/ftl/select.ftl");
template.process(templateContext, env.getOut());
}
页面:
eval:把字符串当做ftl代码处理;处理后对象的值要以string的length来判断,如果直接用??,则对象为空
;
<#list list as obj>
<#if obj??>
<#assign keys = ('obj.'+ optionKey) >
<#assign values = ('obj.'+ optionValue) >
<#if ((values?eval)?length gt 0 && (keys?eval)?length gt 0) >
<option value="${values?eval}" <#if ((value??)&& value == values?eval) >selected</#if>>${keys?eval!''}</option>
</#if>
</#if>
</#list>
最后,还要在项目里注册自定义的标签;
注册:
sothis.properties
sothis.freemarker.directive.d_select.class=com.fangjia.bkoff.util.SelectDirective
底层(部分):
private SothisConfig config;
String configLocation = filterConfig.getInitParameter("configLocation");
if (null == configLocation) {
configLocation = "sothis.properties";
}
config = SothisConfig.initConfig(configLocation);
分享到:
相关推荐
NULL 博文链接:https://carolli.iteye.com/blog/1387704
NULL 博文链接:https://zhenghuazhi.iteye.com/blog/1923544
对freemark描述,请多多下载~~~~~~~~~~~~~~~·····
主要介绍了freemark标签的相关基础知识,基本上了解Freemark标签的应用
freemark标签使用的一些说明.希望对大家有点用。
一些很不错的freemark资料及eclipse的freemark插件
freemark开发指南详解freemark开发指南详解freemark开发指南详解freemark开发指南详解freemark开发指南详解
对freemark一些讲解及运用总结,请思量后下载
freemark中文资料 freemark教程 freemark基础和高级教程
freemark ,
freemark模板&模版技术&freemark模板&模版技术
FREEMARK资料,开发入门指南 是开发人员了解FREEMARK的一本入门资料
FreeMarker 是一个好用的java引擎,本站为您提供的是中文教程资料合集,为您使用和学习FreeMark提供帮助,内容包括:教程、标签、参考手册、根据模版生成文件使用及语法相关小例子等
Freemark指南:全面介绍Freemark的使用方法,让你的页面已程序完美的结合!
java 将html转为word,使用Freemarker 比较好装好。方便好用。也可以转换成pdf格式。
freemark比较实用的手册,中文版 适合快速学习
FreeMark jar包,中文官方文档。FreeMark jar包,中文官方文档。
freemark生成静态页面,直接导入该项目 使用JDK1.6。直接运行代码中的CreateHtmlWebSite.java, 即可在webroot下面生成MyWebSite.html静态页面。