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" %>
<A href="mailto:!--%@tag pageEncoding=" --="" body-content="empty" iselignored="false">!--<A href="mailto:%@tag pageEncoding=" --="" body-content="empty" iselignored="false">%@tag pageEncoding="UTF-8" isELIgnored="false" body-content="empty"%> --</A></A>
<!--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>
<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%@ 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="输入框值"%>
<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(){
top.$.jBox.open("iframe:${ctx}/tag/iconselect?value="+$("#${id}").val(), "选择图标", 700, $(top.document).height()-180, {
buttons:{"确定":"ok", "清除":"clear", "关闭":true}, submit:function(v, h, f){
if (v=="ok"){
var icon = h.find("iframe")[0].contentWindow.$("#icon").val();
$("#${id}Icon").attr("class", "icon-"+icon);
$("#${id}IconLabel").text(icon);
$("#${id}").val(icon);
}else if (v=="clear"){
$("#${id}Icon").attr("class", "icon- hide");
$("#${id}IconLabel").text("无");
$("#${id}").val("");
}
}, loaded:function(h){
$(".jbox-content", top.document).css("overflow-y","hidden");
}
});
});
</script>
<div class="controls">
<tags:iconselect id="icon" name="icon" value="${menu.icon}"></tags:iconselect>
</div>