tag文件只是以tag为后缀名的文本文件。除了jsp页面指令外,其他JSP元素都可以出现在tag文件中
页面引用格式
<%@ taglib prefix="ui" tagdir="/WEB-INF/tags" %>
tagdir:用于指定tag文件目录,当页面使用<ui:xxxx>进,会查找该目录下对应的xxxx.tag文件。
prefix:指定使用时标签前缀
使用格式
<ui:xxxx></ui:xxxx>
例子:<ui:tagDemo><ui:tagDemo>
tag文件添加属性:当tag文件需要引用页面传入参数时,就需要在tag文件中填加属性
定义属性格式
<%@ attribute name="attributename" required="true" type="com.myapp.util.ListPage" %>
name(必须):属性名
required(必须):指定是否必须传
type(可选):指定属性类型。
tag文件获得传入参数值
String attributename=(String) pageContext.getAttribute("attributename");
或者在jsp元素中使用${pageScope.attributename}
也可使用<jsp:doBody/> 获取引用页面标签内的body内容。
下面是示例:
tagDemo.tag
<%@tag pageEncoding="UTF-8" isELIgnored="false" %>
!--%@tag pageEncoding="UTF-8" isELIgnored="false" body-content="empty"%> --
<!--body-content="empty"表明使用标签时,标签内不能有内容 -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ attribute name="listPage" required="true" type="com.myapp.util.ListPage" %>
<%@ attribute name="url" required="true"%>
<%@ attribute name="appender" required="false"%>
<%@ attribute name="color" required="false" %>
<table width="400" cellpadding="5" bgcolor="${pageScope.color}">
<tr>
<td>
<jsp:doBody/>
</td>
</tr>
</table>
<c:if test="${not empty listPage.authors}">
<c:choose>
<c:when test="${not empty appender}">
<c:set var="myPath" value="${url}${appender}page="/>
</c:when>
<c:otherwise>
<c:set var="myPath" value="${url}"/>
</c:otherwise>
</c:choose>
<c:if test="${listPage.hasNext}"><a href='<c:url value="${myPath}${listPage.nextPage}"/>'>下一页</a></c:if>
<c:if test="${listPage.hasPrev}"><a href='<c:url value="${myPath}${listPage.prevPage}"/>'>上一页</a></c:if>
(${listPage.currentPage}/<a href='<c:url value="${myPath}${listPage.totalPage}"/>'>${listPage.totalPage}</a>页)<br/>
<c:if test="${listPage.totalPage >= 3}">
快速翻页:<input name="page" maxlength="4" size="3" value="1" format="*N"/>
<anchor>GO
<go method="post" href="<c:url value='${myPath}$(page)'/>"></go>
</anchor><br/>
</c:if>
</c:if>
使用页面tagDemo.jsp
<%@page import="com.myapp.domain.Author"%> <%@page import="com.myapp.util.ListPage"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>标签文件简介示例</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> <% ListPage listPage=new ListPage(); listPage.setHasNext(true); listPage.setHasPrev(false); listPage.setTotalPage(10); listPage.setCurrentPage(1); listPage.setNextPage(2); listPage.setPrevPage(10); List<Author> atuhors=new ArrayList<Author>(); Author author=new Author(); author.setId(1); author.setName("liao"); atuhors.add(author); listPage.setAuthors(atuhors); %> <tags:tagDemo url="tagDemo.jsp?page=" listPage="<%=listPage%>" color="red">这是我传入的Body内容</tags:tagDemo> </body> </html>