首页 > 代码库 > 开发带属性的标签

开发带属性的标签

 1 package cn.itcast.web.tag; 2  3 import java.io.IOException; 4 import java.io.StringWriter; 5 import java.util.Date; 6  7 import javax.servlet.jsp.JspException; 8 import javax.servlet.jsp.tagext.JspFragment; 9 import javax.servlet.jsp.tagext.SimpleTagSupport;10 11 //开发带属性的内容12 public class SimpleTagDemo1 extends SimpleTagSupport {13     14     private int count;15     private Date date;16     17     public void setDate(Date date) {18         this.date = date;19     }20 21 22     public void setCount(int count){23         this.count=count;24     }25     26     27     @Override28     public void doTag() throws JspException, IOException {29         JspFragment jf = this.getJspBody();30         this.getJspContext().getOut().write(date.toLocaleString()+"<br/>");31         for (int i = 0; i < count; i++) {32             jf.invoke(null);33         }34     }35 36     37 38 }
SimpleTagDemo1
 1 <?xml version="1.0" encoding="UTF-8" ?> 2  3  4 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" 5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 7     version="2.0"> 8     <description>A tag library exercising SimpleTag handlers.</description> 9     <tlib-version>1.0</tlib-version>10     <short-name>itcast</short-name>11     <uri>/itcast</uri>12     13     <tag>14         <name>SimpleTagDemo1</name>15         <tag-class>cn.itcast.web.tag.SimpleTagDemo1</tag-class>16         <body-content>scriptless</body-content>17         18         <attribute>19             <name>count</name>20             <required>true</required>21             <rtexprvalue>true</rtexprvalue>22         </attribute>23         <attribute>24             <name>date</name>25             <required>true</required>26             <rtexprvalue>true</rtexprvalue>27         </attribute>28     </tag>29     30    31   32   </taglib>
itcast.tld
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib uri="/itcast" prefix="itcast" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>        <title>开发带属性的内容</title>    </head>    <body>  <itcast:SimpleTagDemo1 count="9" date="<%=new Date() %>">  	Zero<br/>  </itcast:SimpleTagDemo1>   	  </body></html>