分享

tag标签文件 .

 I_T_馆 2014-07-11

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" %>  

  

<!--body-content="empty"表明使用标签时,标签内不能有内容 -->  

  

<%@ taglib prefix="c" uri="http://java./jsp/jstl/core" %>  

<%@ attribute name="listPage" required="true" type="test.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

<PRE class=html name="code">
<%@page import="test.Author"%>  
<%@page import="test.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>  
</PRE><BR>  
<PRE></PRE>  

  --------------------------------------------------------------------------------------------
下面是两个实体类
ListPage.java


public class ListPage {

private boolean hasNext;
private boolean hasPrev;
private Integer totalPage;
private Integer currentPage;
private Integer nextPage;
private Integer prevPage;
private List<Author> authors;
public List<Author> getAuthors() {
return authors;
}
public void setAuthors(List<Author> authors) {
...
}

 --------------------------------------------------------------------------------------------
Author.java

public class Author {

private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
 --------------------------------------------------------------------------------------------
做这个demo需要导入jstl的标签库 jstl.jar和standard.jar

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多