`

freemark自定义标签

阅读更多
freemark自定义标签:


自定义标签类需SelectDirective implements TemplateDirectiveModel;
eg:public class SelectDirective implements TemplateDirectiveModel
实现execute方法:
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
throws TemplateException, IOException {
}
实现code:
/**
* 自定义下拉框
* @author Administrator
*
*/
public class SelectDirective implements TemplateDirectiveModel {

public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
throws TemplateException, IOException {
//id value noSelection multiple onEventName callBack optionKey optionValue styleStr
//list strList map

// id
String id = MapUtils.getString(params, "id", "selectId");
// value
String value = MapUtils.getString(params, "value");
// noSelection
String noSelection = MapUtils.getString(params, "noSelection");
// multiple
boolean multiple = Boolean.valueOf(MapUtils.getString(params, "multiple"));

// onEventName:默认有callback时为onChange
String onEventName = MapUtils.getString(params, "onEventName","onChange");
// callBack
String callBack = MapUtils.getString(params, "callBack");

// optionKey
String optionKey = MapUtils.getString(params, "optionKey");
// optionValue
String optionValue = MapUtils.getString(params, "optionValue");

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("optionKey", optionKey);
templateContext.put("optionValue", optionValue);

templateContext.put("id", id);
templateContext.put("value", value);

if( StringUtils.isNotBlank(noSelection) ){
String noSelectionKey= noSelection.substring( noSelection.indexOf(":")+1 , noSelection.length());
String noSelectionValue= noSelection.substring(0, noSelection.indexOf(":"));
templateContext.put("noSelectionKey", noSelectionKey);
templateContext.put("noSelectionValue", noSelectionValue );
}

templateContext.put("multiple", multiple);
if(StringUtils.isNotBlank(callBack)){
templateContext.put("onEventStr", onEventName+"='javascript:"+ callBack +"'");//onchange="javascript:submitForm();"
}
Template template = env.getConfiguration().getTemplate("/ftl/select.ftl");
template.process(templateContext, env.getOut());
}

}

页面code:
<#ftl attributes={"content_type":"text/html; charset=UTF-8"}>
<select name="${id}" id="${id}" <#if onEventStr?? && onEventStr?length gt 0 >${onEventStr!''}</#if> <#if multiple?? && multiple==true >multiple="multiple"</#if> >
<#if noSelectionKey??  && noSelectionValue?? !(value??)>
<option value="${noSelectionKey}" <#if ((value??)&& value == noSelectionValue) >selected</#if>>${noSelectionValue}</option>
</#if>

<#if strList?? && strList?size gt 0>
<#list strList! as str>
<#assign keys = t_util.cutString(str, 0, t_util.indexOf(str, ':') ) >
<#assign values = t_util.cutString(str , t_util.indexOf(str, ':')+1 , str?length ) >
<option value= "${values}" <#if value?? && value==values >selected</#if>>${keys}</option>
</#list>
<#elseif list?? && list?size gt 0>
<#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>
<#elseif map?? && map?size gt 0>
<#list map?keys as key>
<#if optionKey?? && optionKey=="value">
<option value="${map[key]!''}" <#if value?? && value== map[key]>selected="selected"</#if> >${key!''}</option>
<#else>
<option value="${key!''}" <#if value?? && value== key>selected="selected"</#if> >${map[key]!''}</option>
</#if>
</#list>
</#if>
</select>


调用code:
<div class="main-content">
<h1 align="center">d_select使用示例:</h1>
<div >
strList:集合为静态字符串集合:</br>
指定字符串:strList="key:value,key:value....",eg:strList="文字:1,图片:2"</br>
</br>
1,指定onchange事件的回调函数:&nbsp;&nbsp;&nbsp;<@d_select id="type_1" strList="文字:1,图片:2" callBack="callbackonChanged();"/>指定callBack为js回调函数名即可,默认有callBack时击发onchange事件,js函数需自己写;</br>
2,指定除onchange的其他事件的回调函数:&nbsp;&nbsp;&nbsp;<@d_select id="type_1" strList="文字:1,图片:2" callBack="callbackonClick();" onEventName="onClick"/>指定callBack为js回调函数名,并且指定事件名onEventName,eg:onEventName="onClick",js函数需自己写;</br>
3,多选:&nbsp;&nbsp;&nbsp;<@d_select id="type_2" strList="文字:1,图片:2" noSelection="请选择:0" value="2" multiple="true"  callBack="callbackonClick();" onEventName="onClick"/>多选:multiple="true";附加选项:noSelection="0:请选择" </br>
4,有默认选中项时:&nbsp;&nbsp;&nbsp;<@d_select id="type_3" strList="文字:1,图片:2" value="2" noSelection="请选择:0" callBack="callbackonChanged();" onEventName="onChange"/>默认选中值:value="2"; </br>
</br></br></br>
list:集合为页面上的list对象:</br>
5,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<@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";</br>
城市:<@d_select id="currentlyCity" optionKey="display" optionValue="name" noSelection="所有:" list=serverAreaList ></@d_select>
</br></br></br>
map:集合为页面上的map对象:</br>
6,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<@d_select id="pageId" optionKey="value" map=pageIdMap callBack="submitForm();" value=""></@d_select>指定map对象名:map=pageIdMap;如果optional的key,value与map的key,value相反,则:optionKey="value",否则,不写。</br>

</div>
<h1 align="center">d_select属性:</h1>
<div>
选项集合:</br>
必须指定,list strList map,且只能有一个</br>
注:集合为list时,需指定optional的key和value对应集合对象的属性名:optionKey optionValue;</br>
为map时,optional的key,value与map不一致时,指定optionKey="value"即可;</br>

其他属性:id value noSelection multiple onEventName callBack optionKey optionValue ;</br>
id:下拉框Id,默认值为:selectId;name属性值与Id相同;</br>
value:默认选中的值;</br>
noSelection:除指定集合的一个额外选项( key:value,空值时eg(:value) );</br>
multiple: 是否多选(true/false);</br>
onEventName:事件名,默认为onChange;</br>
callBack:回调函数名;</br>
optionKey , optionValue:
对应list的属性字段,必须写;</br>
对应map的key,value字段,可以不写(如果map的key对应optional的value,需写为:optionKey="value");
</br> 
</div>
</div>
分享到:
评论

相关推荐

    freemark 自定义标签 总结

    NULL 博文链接:https://carolli.iteye.com/blog/1387747

    freemarker 自定义freeMarker标签

    NULL 博文链接:https://zhenghuazhi.iteye.com/blog/1923544

    campus-market_springboot_后台管理系统_stand3fu_数据权限控制_

    涉及的知识点有:springboot框架原理、freemark模板标签语法、jpa数据库操作及自动建表、统一上传文件实现方法、自定义注解实现统一验证方法、权限拦截器实现权限统一管理、自定义分页插件封装、调用控制台命令对...

    Springboot角色权限后台管理系统脚手架实战开发教程包含完整源码

    系统实现的功能主要有用户管理、角色管理、权限管理、日志管理、数据库备份等等,涉及的知识点有:springboot框架原理、freemark模板标签语法、jpa数据库操作及自动建表、统一上传文件实现方法、自定义注解实现统一...

    freemarker+struts2仿QQ分页效果

    因为现在一般都用strut2,为了有很好的通用性,首先想到的自然是strut2的自定义标签。于是马上google一下。果然,一大堆。不过由于小弟资历浅薄,也没心思看,主要是strut2的标签本来我就觉得不大习惯,另外上个月...

    freemarker语法完整版

    Freemarker页面语法 A 概念 最常用的 3 个概念 sequence 序列,对应java 里的list 、数组等非键值对的集合 hash 键值对的集合 namespace 对一个ftl 文件的引用, 利用这个名字可以访问到该ftl 文件的资源 ...

    freemarker总结

    6、 用户自定义FTL指令:宏和变换器 7、 节点 节点变量表示为树型结构中的一个节点,通常在XML处理中使用。 在模板里对sequences和hashes初始化 sequences 1. [“you”,”me”,”he”] 2. 1..100 3. [ {“Akey...

    1345个易语言模块

    freemark 模块_取文件.ec ftp文件操作模块.ec gdiplus类模块.ec GetStringSize.ec GIF快 照.ec Hex-Dec.ec Hex-Dec1.ec Hex2Dec.ec hide.ec hotkey.ec HTTP.ec http_ec.ec http_ec1.ec HTTP 访问模块 .ec HTTP访问...

    1350多个精品易语言模块

    freemark 模块_取文件.ec ftp文件操作模块.ec gdiplus类模块.ec GetStringSize.ec GIF快 照.ec Hex-Dec.ec Hex-Dec1.ec Hex2Dec.ec hide.ec hotkey.ec HTTP.ec http_ec.ec http_ec1.ec HTTP 访问模块 .ec HTTP访问...

Global site tag (gtag.js) - Google Analytics