JSP 中的 tag 文件
在jsp文件中,可以引用 tag 和tld 文件,本文主要针对 tag
对于tag 文件
1)将此类文件放在 WEB-INF 下,比如 /WEB-INF/tags,tags 是目录,其下可以有多个.tag文件,如 tree.tag,menu.tag
2)在jsp 中使用 <%@ taglib prefix="sys" tagdir="/WEB-INF/tags" %> 来引入
3)在jsp 页面上,采用<sys:xxx> 来使用该 tag 文件,如 <sys:tree>、<sys:menu>
tag 文件的作用一般是一段小代码,类似 include 文件的作用
例:
1、引入 tag 文件
1 | <%@ taglib prefix= "sys" tagdir= "/WEB-INF/tags/sys" %> |
2、在 /WEB-INF/tags/sys 下,新增一个 test.tag 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <%@ tag language= "java" pageEncoding= "UTF-8" %><br><%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %> <%@ attribute name= "id" type= "java.lang.String" required= "true" description= "编号" %> <%@ attribute name= "name" type= "java.lang.String" required= "true" description= "输入框名称" %> <%@ attribute name= "value" type= "java.lang.String" required= "true" description= "输入框值" %> <%@ attribute name= "notAllowSelectParent" type= "java.lang.Boolean" required= "false" description= "不允许选择父节点" %> <i id= "${id}Icon" class = "icon-${not empty value?value:' hide'}" ></i> <label id= "${id}IconLabel" >${not empty value?value: '无' }</label> <input id= "${id}" name= "${name}" type= "hidden" value= "${value}" /><a id= "${id}Button" href= "javascript:" class = "btn" >选择</a> <script type= "text/javascript" > $( "#${id}Button" ).click(function(){ //<c:if test="${notAllowSelectParent}"> alert( "不能选择父节点" ); //</c:if> }); </script> |
3、在jsp 中使用该 tag
1 | <sys:test name= "test" value= "test" id= "test" notAllowSelectParent= "true" ></sys:test> |
4、运行程序,可以发现程序正常运行
问题是:tag 中的 //<c:if test="${notAllowSelectParent}"> 不是被注释掉了吗?为什么还是能正常运行? 为什么js 中可以使用 jstl 标签?
实际运行结果:与注释没有任何关系……
作者:panie
出处:http://www.cnblogs.com/panie2015/
如果您希望与我交流互动,欢迎加我微信
本文内容为作者辛苦整理书写,欢迎转载,但请保留文章出处
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?