7.3.2 include指令

7.3.2 include指令

tag file中的include指令和JSP页面中的include指令是一样的。可以使用这个指令来将外部文件导入到tag file。当你有一个公共资源文件有可能用在多个tag file中时,include指令将能够发挥它的作用。这个公共资源文件可以是静态文件(例如HTML文件),也可以是动态文件(例如其他tag file)。

实例

includeDemoTag.tag

1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<body>
<h2>下面是标签文件includeDemoTag.tag中的内容</h2>
<hr color="green">
<!-- 引入自定义标签目录,取个别名为前缀`easy` -->
<%@ taglib prefix="easy" tagdir="/WEB-INF/tags"%>
<!-- 引入前缀`easy`所表示的标签目录中的`includeDemoTag.tag`文件 -->
<easy:includeDemoTag />
<hr color="green">
</body>
</html>

includeDemoTag.tag

1
2
3
4
5
6
7
8
9
10
<!-- 设置tag文件的编码 -->
<%@ tag pageEncoding="utf-8"%>
<strong>以下是included2.tagf的内容:</strong>
<hr color="red">
<%@ include file="included2.tagf"%>
<hr color="red">
<hr color="blue">
<strong>下面是included.tagf文件中的内容:</strong>
<%@ include file="included.tagf"%>
<hr color="blue">

included2.tagf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@tag pageEncoding="utf-8"%>
<table>
<tr>
<th>水果</th>
<th>单价(斤)</th>
</tr>
<tr>
<td>苹果</td>
<td>4.8</td>
</tr>
<tr>
<td>香蕉</td>
<td>3.6</td>
</tr>
<tr>
<td>草莓</td>
<td>12.5</td>
</tr>
</table>

included.tagf

1
2
3
4
<%@ tag pageEncoding="utf-8"%>
<%
out.print("<strong>included.tagf文件中的内容</strong>");
%>

可以通过下面的URL来访问includeDemoTagTest.jsp页面:
http://localhost:8080/app07a/includeDemoTagTest.jsp
显示效果如下:
这里有一张图片
关于include指令更详细的介绍,可以查看第3章。