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>







原文地址:https://www.cnblogs.com/signheart/p/f1d1b95006bfb85b1bfa65361789cd77.html