tag标签文件

http://blog.csdn.net/liaoxiaohua1981/article/details/6856108

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

  1. <%@tag pageEncoding="UTF-8" isELIgnored="false" %>  
  2. <a href="mailto:!--%@tag pageEncoding=" iselignored="false" body-content="empty" --="">!--</a><a href="mailto:%@tag pageEncoding=" iselignored="false" body-content="empty" --="">%@tag pageEncoding="UTF-8" isELIgnored="false" body-content="empty"%> --</a>  
  3. <!--body-content="empty"表明使用标签时,标签内不能有内容 -->  
  4.   
  5. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  6. <%@ attribute name="listPage" required="true" type="com.myapp.util.ListPage" %>  
  7. <%@ attribute name="url" required="true"%>  
  8. <%@ attribute name="appender" required="false"%>  
  9. <%@ attribute name="color" required="false" %>  
  10. <table width="400" cellpadding="5"  bgcolor="${pageScope.color}">   
  11.      <tr>   
  12.          <td>   
  13.              <jsp:doBody/>  
  14.          </td>   
  15.      </tr>   
  16. </table>  
  17. <c:if test="${not empty listPage.authors}">  
  18.     <c:choose>  
  19.         <c:when test="${not empty appender}">  
  20.             <c:set var="myPath" value="${url}${appender}page="/>  
  21.         </c:when>  
  22.         <c:otherwise>  
  23.             <c:set var="myPath" value="${url}"/>  
  24.         </c:otherwise>  
  25.     </c:choose>  
  26.     <c:if test="${listPage.hasNext}"><a href='<c:url value="${myPath}${listPage.nextPage}"/>'>下一页</a></c:if>   
  27.     <c:if test="${listPage.hasPrev}"><a href='<c:url value="${myPath}${listPage.prevPage}"/>'>上一页</a></c:if>  
  28.     (${listPage.currentPage}/<a href='<c:url value="${myPath}${listPage.totalPage}"/>'>${listPage.totalPage}</a>页)<br/>  
  29.     <c:if test="${listPage.totalPage >= 3}">  
  30.         快速翻页:<input name="page" maxlength="4" size="3" value="1" format="*N"/>  
  31.         <anchor>GO  
  32.             <go method="post" href="<c:url value='${myPath}$(page)'/>"></go>  
  33.         </anchor><br/>  
  34.     </c:if>  
  35. </c:if>  
  1.    
  1. 使用页面tagDemo.jsp  
  1. <pre class="html" name="code"><%@page import="com.myapp.domain.Author"%>  
  2. <%@page import="com.myapp.util.ListPage"%>  
  3. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  4. <%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>   
  5.   
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.      
  11.       
  12.     <title>标签文件简介示例</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.   <%  
  27.   ListPage listPage=new ListPage();  
  28.   listPage.setHasNext(true);  
  29.   listPage.setHasPrev(false);  
  30.   listPage.setTotalPage(10);     
  31.   listPage.setCurrentPage(1);  
  32.   listPage.setNextPage(2);  
  33.   listPage.setPrevPage(10);  
  34.   List<Author> atuhors=new ArrayList<Author>();  
  35.   Author author=new Author();  
  36.   author.setId(1);  
  37.   author.setName("liao");  
  38.   atuhors.add(author);  
  39.   listPage.setAuthors(atuhors);  
  40.    %>  
  41.     <tags:tagDemo url="tagDemo.jsp?page=" listPage="<%=listPage%>" color="red">这是我传入的Body内容</tags:tagDemo>  
  42.   </body>  
  43. </html>  
  44. </pre><br>  
  45. <pre></pre> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值