引入
<%@ taglib prefix="sys" tagdir="/WEB-INF/tags/sys" %>
這里注意/WEB-INF/tags/sys這里是一個目錄,目錄下有很多tag文件如下,調用sys:xxx,就會在當前目錄下找xxx.tag的文件,必須有對應xxx.tag的文件與之對應
<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %>
<%@ attribute name="typeCode" type="java.lang.String" required="true" description="字典code"%>
<%@ attribute name="defaultValue" type="java.lang.String" description="默認選中"%>
<%@ attribute name="style" type="java.lang.String" description="默認選中"%>
<%@ attribute name="cls" type="java.lang.String" description="默認選中"%>
<%@ attribute name="name" type="java.lang.String" description="默認選中"%>
<select style="${style}" class="${cls}" name="${name}" id="${name}" >
<option value="" >請選擇... </option>
<c:if test="${not empty typeCode}">
<c:forEach items="${fns:getDictList(typeCode)}" var='dict'>
<option value='${dict.VALUE}' ${defaultValue==dict.VALUE?'selected':''}>${dict.TEXT}</option>
</c:forEach>
</c:if>
</select>
attribute的屬性介紹如下:
1. name :這個attribute的名稱.
2. required : true/false, 是否必須的.
3. rtexprvalue : true/false, 這個attribute可否使用EL表達式, 否則為純文本.
4. type : 設定這個attribute的類型, jsp容器會把結果自動轉換成這個類.
如此,jsp名就是標簽名,例如這個jsp叫 select.jsp,那么它的用法就是
<sys:select cls="formselect" name="MODULE_TYPE" typeCode="HOME_MODULE_TYPE" defaultValue="${record.MODULE_TYPE }" />
附上一個實際例子
treeselect.tag
<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%@ attribute name="id" type="java.lang.String" required="true" description="編號"%>
<%@ attribute name="name" type="java.lang.String" required="true" description="隱藏域名稱(ID)"%>
<%@ attribute name="value" type="java.lang.String" required="true" description="隱藏域值(ID)"%>
<%@ attribute name="labelName" type="java.lang.String" required="true" description="輸入框名稱(Name)"%>
<%@ attribute name="labelValue" type="java.lang.String" required="true" description="輸入框值(Name)"%>
<%@ attribute name="title" type="java.lang.String" required="true" description="選擇框標題"%>
<%@ attribute name="url" type="java.lang.String" required="true" description="樹結構數據地址"%>
<%@ attribute name="checked" type="java.lang.Boolean" required="false" description="是否顯示復選框,如果不需要返回父節點,請設置notAllowSelectParent為true"%>
<%@ attribute name="extId" type="java.lang.String" required="false" description="排除掉的編號(不能選擇的編號)"%>
<%@ attribute name="isAll" type="java.lang.Boolean" required="false" description="是否列出全部數據,設置true則不進行數據權限過濾(目前僅對Office有效)"%>
<%@ attribute name="notAllowSelectRoot" type="java.lang.Boolean" required="false" description="不允許選擇根節點"%>
<%@ attribute name="notAllowSelectParent" type="java.lang.Boolean" required="false" description="不允許選擇父節點"%>
<%@ attribute name="module" type="java.lang.String" required="false" description="過濾欄目模型(只顯示指定模型,僅針對CMS的Category樹)"%>
<%@ attribute name="selectScopeModule" type="java.lang.Boolean" required="false" description="選擇范圍內的模型(控制不能選擇公共模型,不能選擇本欄目外的模型)(僅針對CMS的Category樹)"%>
<%@ attribute name="allowClear" type="java.lang.Boolean" required="false" description="是否允許清除"%>
<%@ attribute name="allowInput" type="java.lang.Boolean" required="false" description="文本框可填寫"%>
<%@ attribute name="cssClass" type="java.lang.String" required="false" description="css樣式"%>
<%@ attribute name="cssStyle" type="java.lang.String" required="false" description="css樣式"%>
<%@ attribute name="smallBtn" type="java.lang.Boolean" required="false" description="縮小按鈕顯示"%>
<%@ attribute name="hideBtn" type="java.lang.Boolean" required="false" description="是否顯示按鈕"%>
<%@ attribute name="disabled" type="java.lang.String" required="false" description="是否限制選擇,如果限制,設置為disabled"%>
<%@ attribute name="dataMsgRequired" type="java.lang.String" required="false" description=""%>
<div class="input-append">
<input id="${id}Id" name="${name}" class="${cssClass}" type="hidden" value="${value}"/>
<input id="${id}Name" name="${labelName}" ${allowInput?'':'readonly="readonly"'} type="text" value="${labelValue}" data-msg-required="${dataMsgRequired}"
class="${cssClass}" style="${cssStyle}"/><a id="${id}Button" href="javascript:" class="btn ${disabled} ${hideBtn ? 'hide' : ''}" style="${smallBtn?'padding:4px 2px;':''}"> <i class="icon-search"></i> </a>
</div>
<script type="text/javascript">
$("#${id}Button, #${id}Name").click(function(){
// 是否限制選擇,如果限制,設置為disabled
if ($("#${id}Button").hasClass("disabled")){
return true;
}
// 正常打開
top.$.jBox.open("iframe:${ctx}/tag/treeselect?url="+encodeURIComponent("${url}")+"&module=${module}&checked=${checked}&extId=${extId}&isAll=${isAll}", "選擇${title}", 300, 420, {
ajaxData:{selectIds: $("#${id}Id").val()},buttons:{"確定":"ok", ${allowClear?"\"清除\":\"clear\", ":""}"關閉":true}, submit:function(v, h, f){
if (v=="ok"){
var tree = h.find("iframe")[0].contentWindow.tree;//h.find("iframe").contents();
var ids = [], names = [], nodes = [];
if ("${checked}" == "true"){
nodes = tree.getCheckedNodes(true);
}else{
nodes = tree.getSelectedNodes();
}
for(var i=0; i<nodes.length; i++) {//<c:if test="${checked && notAllowSelectParent}">
if (nodes[i].isParent){
continue; // 如果為復選框選擇,則過濾掉父節點
}//</c:if><c:if test="${notAllowSelectRoot}">
if (nodes[i].level == 0){
top.$.jBox.tip("不能選擇根節點("+nodes[i].name+")請重新選擇。");
return false;
}//</c:if><c:if test="${notAllowSelectParent}">
if (nodes[i].isParent){
top.$.jBox.tip("不能選擇父節點("+nodes[i].name+")請重新選擇。");
return false;
}//</c:if><c:if test="${not empty module && selectScopeModule}">
if (nodes[i].module == ""){
top.$.jBox.tip("不能選擇公共模型("+nodes[i].name+")請重新選擇。");
return false;
}else if (nodes[i].module != "${module}"){
top.$.jBox.tip("不能選擇當前欄目以外的欄目模型,請重新選擇。");
return false;
}//</c:if>
ids.push(nodes[i].id);
names.push(nodes[i].name);//<c:if test="${!checked}">
break; // 如果為非復選框選擇,則返回第一個選擇 </c:if>
}
$("#${id}Id").val(ids.join(",").replace(/u_/ig,""));
$("#${id}Name").val(names.join(","));
}//<c:if test="${allowClear}">
else if (v=="clear"){
$("#${id}Id").val("");
$("#${id}Name").val("");
}//</c:if>
if(typeof ${id}TreeselectCallBack == 'function'){
${id}TreeselectCallBack(v, h, f);
}
}, loaded:function(h){
$(".jbox-content", top.document).css("overflow-y","hidden");
}
});
});
</script>