jsp tag 直接文件實現



  
  
  
          
引入

 
 
 
         
   
   
   
           
  1. <%@ taglib prefix="sys" tagdir="/WEB-INF/tags/sys" %>

這里注意/WEB-INF/tags/sys這里是一個目錄,目錄下有很多tag文件如下,調用sys:xxx,就會在當前目錄下找xxx.tag的文件,必須有對應xxx.tag的文件與之對應

 
 
 
         
   
   
   
           
  1. <%@ tag language="java" pageEncoding="UTF-8"%>
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  3. <%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %>
  4. <%@ attribute name="typeCode" type="java.lang.String" required="true" description="字典code"%>
  5. <%@ attribute name="defaultValue" type="java.lang.String" description="默認選中"%>
  6. <%@ attribute name="style" type="java.lang.String" description="默認選中"%>
  7. <%@ attribute name="cls" type="java.lang.String" description="默認選中"%>
  8. <%@ attribute name="name" type="java.lang.String" description="默認選中"%>
  9. <select style="${style}" class="${cls}" name="${name}" id="${name}" >
  10. <option value="" >請選擇... </option>
  11. <c:if test="${not empty typeCode}">
  12. <c:forEach items="${fns:getDictList(typeCode)}" var='dict'>
  13. <option value='${dict.VALUE}' ${defaultValue==dict.VALUE?'selected':''}>${dict.TEXT}</option>
  14. </c:forEach>
  15. </c:if>
  16. </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

  
  
  
          
  1. <%@ tag language="java" pageEncoding="UTF-8"%>
  2. <%@ include file="/WEB-INF/views/include/taglib.jsp"%>
  3. <%@ attribute name="id" type="java.lang.String" required="true" description="編號"%>
  4. <%@ attribute name="name" type="java.lang.String" required="true" description="隱藏域名稱(ID)"%>
  5. <%@ attribute name="value" type="java.lang.String" required="true" description="隱藏域值(ID)"%>
  6. <%@ attribute name="labelName" type="java.lang.String" required="true" description="輸入框名稱(Name)"%>
  7. <%@ attribute name="labelValue" type="java.lang.String" required="true" description="輸入框值(Name)"%>
  8. <%@ attribute name="title" type="java.lang.String" required="true" description="選擇框標題"%>
  9. <%@ attribute name="url" type="java.lang.String" required="true" description="樹結構數據地址"%>
  10. <%@ attribute name="checked" type="java.lang.Boolean" required="false" description="是否顯示復選框,如果不需要返回父節點,請設置notAllowSelectParent為true"%>
  11. <%@ attribute name="extId" type="java.lang.String" required="false" description="排除掉的編號(不能選擇的編號)"%>
  12. <%@ attribute name="isAll" type="java.lang.Boolean" required="false" description="是否列出全部數據,設置true則不進行數據權限過濾(目前僅對Office有效)"%>
  13. <%@ attribute name="notAllowSelectRoot" type="java.lang.Boolean" required="false" description="不允許選擇根節點"%>
  14. <%@ attribute name="notAllowSelectParent" type="java.lang.Boolean" required="false" description="不允許選擇父節點"%>
  15. <%@ attribute name="module" type="java.lang.String" required="false" description="過濾欄目模型(只顯示指定模型,僅針對CMS的Category樹)"%>
  16. <%@ attribute name="selectScopeModule" type="java.lang.Boolean" required="false" description="選擇范圍內的模型(控制不能選擇公共模型,不能選擇本欄目外的模型)(僅針對CMS的Category樹)"%>
  17. <%@ attribute name="allowClear" type="java.lang.Boolean" required="false" description="是否允許清除"%>
  18. <%@ attribute name="allowInput" type="java.lang.Boolean" required="false" description="文本框可填寫"%>
  19. <%@ attribute name="cssClass" type="java.lang.String" required="false" description="css樣式"%>
  20. <%@ attribute name="cssStyle" type="java.lang.String" required="false" description="css樣式"%>
  21. <%@ attribute name="smallBtn" type="java.lang.Boolean" required="false" description="縮小按鈕顯示"%>
  22. <%@ attribute name="hideBtn" type="java.lang.Boolean" required="false" description="是否顯示按鈕"%>
  23. <%@ attribute name="disabled" type="java.lang.String" required="false" description="是否限制選擇,如果限制,設置為disabled"%>
  24. <%@ attribute name="dataMsgRequired" type="java.lang.String" required="false" description=""%>
  25. <div class="input-append">
  26. <input id="${id}Id" name="${name}" class="${cssClass}" type="hidden" value="${value}"/>
  27. <input id="${id}Name" name="${labelName}" ${allowInput?'':'readonly="readonly"'} type="text" value="${labelValue}" data-msg-required="${dataMsgRequired}"
  28. class="${cssClass}" style="${cssStyle}"/><a id="${id}Button" href="javascript:" class="btn ${disabled} ${hideBtn ? 'hide' : ''}" style="${smallBtn?'padding:4px 2px;':''}">&nbsp;<i class="icon-search"></i>&nbsp;</a>&nbsp;&nbsp;
  29. </div>
  30. <script type="text/javascript">
  31. $("#${id}Button, #${id}Name").click(function(){
  32. // 是否限制選擇,如果限制,設置為disabled
  33. if ($("#${id}Button").hasClass("disabled")){
  34. return true;
  35. }
  36. // 正常打開
  37. top.$.jBox.open("iframe:${ctx}/tag/treeselect?url="+encodeURIComponent("${url}")+"&module=${module}&checked=${checked}&extId=${extId}&isAll=${isAll}", "選擇${title}", 300, 420, {
  38. ajaxData:{selectIds: $("#${id}Id").val()},buttons:{"確定":"ok", ${allowClear?"\"清除\":\"clear\", ":""}"關閉":true}, submit:function(v, h, f){
  39. if (v=="ok"){
  40. var tree = h.find("iframe")[0].contentWindow.tree;//h.find("iframe").contents();
  41. var ids = [], names = [], nodes = [];
  42. if ("${checked}" == "true"){
  43. nodes = tree.getCheckedNodes(true);
  44. }else{
  45. nodes = tree.getSelectedNodes();
  46. }
  47. for(var i=0; i<nodes.length; i++) {//<c:if test="${checked && notAllowSelectParent}">
  48. if (nodes[i].isParent){
  49. continue; // 如果為復選框選擇,則過濾掉父節點
  50. }//</c:if><c:if test="${notAllowSelectRoot}">
  51. if (nodes[i].level == 0){
  52. top.$.jBox.tip("不能選擇根節點("+nodes[i].name+")請重新選擇。");
  53. return false;
  54. }//</c:if><c:if test="${notAllowSelectParent}">
  55. if (nodes[i].isParent){
  56. top.$.jBox.tip("不能選擇父節點("+nodes[i].name+")請重新選擇。");
  57. return false;
  58. }//</c:if><c:if test="${not empty module && selectScopeModule}">
  59. if (nodes[i].module == ""){
  60. top.$.jBox.tip("不能選擇公共模型("+nodes[i].name+")請重新選擇。");
  61. return false;
  62. }else if (nodes[i].module != "${module}"){
  63. top.$.jBox.tip("不能選擇當前欄目以外的欄目模型,請重新選擇。");
  64. return false;
  65. }//</c:if>
  66. ids.push(nodes[i].id);
  67. names.push(nodes[i].name);//<c:if test="${!checked}">
  68. break; // 如果為非復選框選擇,則返回第一個選擇 </c:if>
  69. }
  70. $("#${id}Id").val(ids.join(",").replace(/u_/ig,""));
  71. $("#${id}Name").val(names.join(","));
  72. }//<c:if test="${allowClear}">
  73. else if (v=="clear"){
  74. $("#${id}Id").val("");
  75. $("#${id}Name").val("");
  76. }//</c:if>
  77. if(typeof ${id}TreeselectCallBack == 'function'){
  78. ${id}TreeselectCallBack(v, h, f);
  79. }
  80. }, loaded:function(h){
  81. $(".jbox-content", top.document).css("overflow-y","hidden");
  82. }
  83. });
  84. });
  85. </script>








免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM