笔记——标记文件

来源:互联网 发布:关于白蛇传的网络歌曲 编辑:程序博客网 时间:2024/06/02 20:30

JSP页面

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" %>

<%@ taglib prefix="myTags" tagdir="/WEB-INF/tags" %>
<!-- 定制标记要在TLD中指定,但标记文件属性并不在TLD中指定 -->




<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
  <myTags:FontColor fontColor="#660099"><%--标记文件发送的是标记属性 --%>
  fontColor Test

  </myTags:FontColor>

<%--所有的标记属性都只有标记作用域,一旦标记结束,标记属性就会出作用域 --%>

  </body>

</html>


标记文件(标记属性在此声明)

<%@ attribute name="fontColor" required="true" rtexprvalue="true" %>
<%@ tag body-content="tagdependent" %>
<h3>FontColor Test</h3>
<em><strong>
<font color="${fontColor }"><jsp:doBody /></font>
</strong></em>


运行结果



 

标记文件的标记体中不能使用脚本代码,但标记文件中可以写脚本

scriptless(默认)

empty      

tagdependent(标记体看做纯文本)


尽管标记文件是一个.tag文件,但最后作为一个JSP的一部分,可以使用隐式对象request和response.而且能访问JspContext.但是,不能访问ServletContext

(如果使用脚本,还可以使用一般的EL隐式对象)


值得一提的是,如果建立了一个引用了标记文件的TLD,容器会认为同一个TLD中提到的标记文件和定制标记都属于同一个库



容器查找标记文件


1.直接在WEB-INF/tags目录中查找

2.在WEB-INF/tags的子目录中查找

3.在WEB-INF/lib下一个JAR文件的META-INF/tags目录中查找

4.在WEB-INF/lib下一个JAR文件的META-INF/tags的子目录中查找

5.容器会在4个位置查找标记文件。如果标记文件在一个JAR中部署,就必须有一个TLD来描述他的位置,如

 <tag-file>
  <name>FontColor</name>
  <path>/META-INF/tags/FontColor.tag</path>
 </tag-file>


0 0