JSP2.0自定义标签

来源:互联网 发布:张成泽犬决 知乎 编辑:程序博客网 时间:2024/04/30 17:09

JSP1.0中可以通过继承TagSupport或者BodyTagSupport来实现自定义的tag处理方法。

JSP2.0中也支持另外一种更为简单的自定tag的方法,那就是直接讲JSP代码保存成*.tag或者*.tagx的标签定义文件。tag和tagx文件不仅支持经典jsp代码,各种标签模版代码,还支持xml样式的jsp指令代码。

按照约定,tag和tagx文件需要放置在WEB-INF/tags目录下。

下面是一些简单的示例:

1.简单地显示时间time.tag

[java] view plaincopy
  1. <%@ tag import="java.util.*" import="java.text.*" %>  
  2. <%  
  3.   DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);  
  4.   Date d = new Date(System.currentTimeMillis());  
  5.   out.println(df.format(d));  
  6. %>  
[html] view plaincopy
  1. <%@ taglib prefix="util" tagdir="/WEB-INF/tags" %>  
  2. <html>  
  3.   <head>  
  4.   </head>  
  5.   <body>  
  6.      Today is <util:time/>.  
  7.   </body>  
  8. </html>  
2.复制字符串多少遍repeater.tag

[java] view plaincopy
  1. <%@ attribute name="count" type="java.lang.Integer" required="true" %>  
  2. <%@ attribute name="value" type="java.lang.String" required="true" %>  
  3. <%!  
  4.   private String repeater(Integer count, String s) {  
  5.     int n = count.intValue();  
  6.     StringBuffer sb = new StringBuffer();  
  7.     for (int i = 0; i < n; i++) {  
  8.       sb.append(s);  
  9.     }  
  10.     return sb.toString();  
  11.   }  
  12. %>  
  13. <%  
  14.   out.println(repeater(count, value));  
  15. %>  
[html] view plaincopy
  1. <%@ taglib prefix="util" tagdir="/WEB-INF/tags" %>  
  2. <html>  
  3.   <head>  
  4.   </head>  
  5.   <body>  
  6.     Let's get some sleep! <util:repeater count='${3 * 10}' value='zzz'/>  
  7.   </body>  
  8. </html>  
3.查找省份lookup.tag

[java] view plaincopy
  1. <%@ tag import="java.util.*" %>  
  2. <%@ attribute name="cityName" required="true" %>  
  3. <%@ variable name-given="province" %>  
  4. <%@ variable name-given="population" variable-class="java.lang.Integer" %>  
  5. <%  
  6.   if ("Toronto".equals(cityName)) {  
  7.     jspContext.setAttribute("province""Ontario");  
  8.     jspContext.setAttribute("population"new Integer(2553400));  
  9.   }  
  10.   else if ("Montreal".equals(cityName)) {  
  11.     jspContext.setAttribute("province""Quebec");  
  12.     jspContext.setAttribute("population"new Integer(2195800));  
  13.   }  
  14.   else {  
  15.     jspContext.setAttribute("province""Unknown");  
  16.     jspContext.setAttribute("population"new Integer(-1));  
  17.   }  
  18. %>  
  19. <jsp:doBody/>  
[html] view plaincopy
  1. <%@ taglib prefix="util" tagdir="/WEB-INF/tags" %>  
  2. <html>  
  3.   <head>  
  4.   </head>  
  5.   <body>  
  6.     <% pageContext.setAttribute("cityName", "Montreal"); %>  
  7.     <util:lookup cityName="${cityName}">  
  8.       ${cityName}'s province is ${province}.  
  9.       ${cityName}'s population is approximately ${population / 1000000} million.  
  10.     </util:lookup>  
  11.   </body>  
  12. </html>  
上面的都是使用的经典jsp代码,下面将第3个示例使用其他代码实现:

*使用标签:

[java] view plaincopy
  1. <%@ tag import="java.util.*" %>  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  3. <%@ attribute name="cityName" required="true" %>  
  4. <%@ variable name-given="province" %>  
  5. <%@ variable name-given="population" %>  
  6. <c:choose>  
  7.   <c:when test="cityName eq 'Toronto'>  
  8.     <c:set var="province" value="Ontario"/>  
  9.     <c:set var="population" value="2553400"/>  
  10.   </c:when>  
  11.   <c:when test="cityName eq 'Montreal'>  
  12.     <c:set var="province" value="Quebec"/>  
  13.     <c:set var="population" value="2195800"/>  
  14.   </c:when>  
  15.   <c:otherwise>  
  16.     <c:set var="province" value="Unknown"/>  
  17.     <c:set var="population" value="-1"/>  
  18.   </c:otherwise>  
  19. </c:choose>  
  20. %>  
  21. <jsp:doBody/>  
[html] view plaincopy
  1. <%@ taglib prefix="util" tagdir="/WEB-INF/tags" %>  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  3. <html>  
  4.   <head>  
  5.   </head>  
  6.   <body>  
  7.     <c:set var="cityName" value="Montreal"/>  
  8.     <util:lookup cityName="${cityName}">  
  9.       ${cityName}'s province is ${province}.  
  10.       ${cityName}'s population is approximately ${population / 1000000} million.  
  11.     </util:lookup>  
  12.   </body>  
  13. </html>  
*使用jsp指令,通常这种方式生成xml格式的文件

[html] view plaincopy
  1. <?xml version='1.0'?>  
  2. <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page">  
  3.     <jsp:directive.tag import="java.util.*"/>  
  4.     <jsp:directive.taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"/>  
  5.     <jsp:directive.attribute name="cityName" required="true"/>  
  6.     <jsp:directive.variable name-given="province"/>  
  7.     <jsp:directive.variable name-given="population"/>  
  8.     <c:choose>  
  9.       <c:when test="cityName eq 'Toronto'>  
  10.         <c:set var="province" value="Ontario"/>  
  11.         <c:set var="population" value="2553400"/>  
  12.       </c:when>  
  13.       <c:when test="cityName eq 'Montreal'>  
  14.         <c:set var="province" value="Quebec"/>  
  15.         <c:set var="population" value="2195800"/>  
  16.       </c:when>  
  17.       <c:otherwise>  
  18.         <c:set var="province" value="Unknown"/>  
  19.         <c:set var="population" value="-1"/>  
  20.       </c:otherwise>  
  21.     </c:choose>  
  22. </jsp:root>  
[html] view plaincopy
  1. <?xml version='1.0'?>  
  2. <jsp:root version='2.0'  
  3.           xmlns:jsp="http://java.sun.com/JSP/Page"  
  4.           xmlns:util="urn:jsptagdir:/WEB-INF/tags"  
  5.           xmlns:c="http://java.sun.com/jsp/jstl/core">  
  6.   <jsp:directive.page contentType="text/html"/>            
  7.     <html>  
  8.       <head>  
  9.       </head>  
  10.       <body>  
  11.         <c:set var="cityName" value="Montreal"/>  
  12.         <util:lookup cityName="${cityName}">  
  13.           ${cityName}'s province is ${province}.  
  14.           ${cityName}'s population is approximately ${population / 1000000} million.  
  15.         </util:lookup>  
  16.       </body>  
  17.     </html>  
  18. </jsp:root>  


附录:

标签文件中常用的指令:

tag类似JSP page指令,可以用于import常用的java类库等include导入其他的标签定义文件taglib使用其他标签,如jstl, spring tag, struts tag等等attribute定义一个属性variable定义一个jsp page中可见的变量,默认范围为NESTED,表示标签内有效。可选项有AT_BEGIN和AT_END
这些指令的可选属性

body-content标签body的处理方式 ,可选项: 'empty', 'tagdependent' or 'scriptless'import导入使用的java类库pageEncoding设置页面编码isELIgnored是否忽略el表达式dynamic-attributes用于存储自定义属性的map,所谓的自定义属性指:未隐式申明的变量language使用的脚本语言,目前必须是javadisplay-name标签名small-iconfor toolslarge-iconfor toolsdescription标签作用描述exampleinformal description of how the tag is used<jsp:directive:attribute>的可选属性

name属性名requiredtrue or falsertexprvaluetrue or false - 指定是否支持运行时表达式type值类型 - 默认是java.lang.Stringfragmenttrue or false - 值先传递给容器(false), 直接传给标签处理方法(true)description属性描述
<jsp:directive:variable>的可选属性

name-given变量名(标签使用时的变量名)name-from-attributeSpecifies the name of an attribute, whose value is the name of the variable that will be available in the calling JSP page. Exactly one of name-given or name-from-attribute must be supplied.aliasA locally scoped variable which will store the variable's value. Used only with name-from-attribute.variable-class变量类.默认是java.lang.String.declareIndicates whether the variable is declared in the calling JSP page or tag file. Default is true. Not entirely clear what this means!scope变量范围,可选项 AT_BEGIN(标签后jsp page内有效), AT_END(标签后jsp page内有效) and NESTED. NESTED(默认,标签内有效)description变量描述

0 0
原创粉丝点击