JSP标签发开

1、开发自定义标签类,该类继承自javax.servlet.jsp.tagext.SimpleTagSupport 
    如果标签类包含属性,必须有对应的setter和getter方法 
    重写doTag()方法,负责生成页面内容 
Java代码   收藏代码
  1. package com.royzhou.hello;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.jsp.JspException;  
  6. import javax.servlet.jsp.tagext.SimpleTagSupport;  
  7.   
  8. public class HelloTag extends SimpleTagSupport{  
  9.   
  10.     private String userName;  
  11.       
  12.     public String getUserName() {  
  13.         return userName;  
  14.     }  
  15.   
  16.     public void setUserName(String userName) {  
  17.         this.userName = userName;  
  18.     }  
  19.   
  20.     public void doTag() throws JspException, IOException {  
  21.         getJspContext().getOut().write("Welcome " + userName + "!");  
  22.     }  
  23. }  


2、建立Tld文件,路径为web-inf目录下 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.       
  3.     <taglib xmlns="http://java.sun.com/xml/ns/j2ee"  
  4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"  
  6.         version="2.0">  
  7.     <tlib-version>1.0</tlib-version>//版本信息  
  8.     <short-name>myTagLib</short-name>//简写  
  9.     <uri>http://www.royzhou.com/myTagLib</uri>//指定标签库的URI,重要属性,在使用标签库的时候用到  
  10.       
  11.     标签库可包含多个tag  
  12.     <tag>  
  13.         <name>HelloWorld</name>  //标签名称  
  14.         <tag-class>com.royzhou.hello.HelloTag</tag-class>  //处理类  
  15.         <body-content>empty</body-content>  //常用的是empty和scriptless  
  16.             //标签体内容(4种)  
  17.             //tagdependent:标签体内容 直接被写入BodyContent,由自定义标签类来进行处理,而不被JSP容器解释,   
  18.             //  <test:myList>   
  19.             //      select name,age from users   
  20.             //  </test:myList>   
  21.             //JSP:接受所有JSP语法  
  22.             //  <my:test>   
  23.             //      <%=request.getProtocol()%>  
  24.             //  </my:test>   
  25.             //empty:空标记,即起始标记和结束标记之间没有内容。   
  26.             //  <test:mytag />   
  27.             //  <test:mytag uname="Tom" />   
  28.             //  <test:mytag></test:mytag>   
  29.             //scriptless:静态HTML或者表达式语言,但不能是JSP脚本  
  30.           
  31.         <attribute>  
  32.             <name>userName</name>  
  33.             <required>true</required>  //该属性是否必须  
  34.             <fragment>true</fragment> //该属性是否支持JSP脚本  
  35.             //rtexprvalue: 由请求时表达式来指定属性的值,默认为false,如下必须设置为true:   
  36.               <test:welcome uname="<%=request.getParameter("username") %>" />  
  37.             <rtexprvalue>true</rtexprvalue>  
  38.         </attribute>  
  39.     </tag>  
  40.     </taglib>  


3、使用标签库 
在页面中引入<%@ taglib="myTag" uri="http://www.royzhou.com/myTagLib" %> 
使用<mytag:HelloWorld userName="royzhou" />输出可以预览效: 

4、带标签体的标签 
迭代器标签开发处理类 
Java代码   收藏代码
  1. package com.royzhou.hello;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Collection;  
  5.   
  6. import javax.servlet.jsp.JspException;  
  7. import javax.servlet.jsp.PageContext;  
  8. import javax.servlet.jsp.tagext.SimpleTagSupport;  
  9.   
  10. public class IteratorTag extends SimpleTagSupport {  
  11.   
  12.     private String collection;  
  13.       
  14.     private String item;  
  15.       
  16.     public void doTag() throws JspException, IOException {  
  17.         //JspContext是pageContext的父类,可以通过向上转型获取pageContext对象  
  18.         PageContext pageContext = (PageContext) getJspContext();  
  19.         Collection itemList = (Collection)pageContext.getRequest().getAttribute(collection);  
  20.         for(Object s : itemList) {  
  21.             //设置属性到pageContext中  
  22.             getJspContext().setAttribute(item, s);  
  23.             //输出标签体  
  24.             getJspBody().invoke(null);  
  25.         }  
  26.     }  
  27.   
  28.     public String getCollection() {  
  29.         return collection;  
  30.     }  
  31.   
  32.     public void setCollection(String collection) {  
  33.         this.collection = collection;  
  34.     }  
  35.   
  36.     public String getItem() {  
  37.         return item;  
  38.     }  
  39.   
  40.     public void setItem(String item) {  
  41.         this.item = item;  
  42.     }  
  43. }  


tld文件配置 
Java代码   收藏代码
  1. <tag>  
  2.         <name>Iterator</name>  
  3.         <tag-class>com.royzhou.hello.IteratorTag</tag-class>  
  4.         <body-content>scriptless</body-content>  
  5.         <attribute>  
  6.             <name>collection</name>  
  7.             <required>true</required>  
  8.             <fragment>true</fragment>  
  9.         </attribute>  
  10.         <attribute>  
  11.             <name>item</name>  
  12.             <required>true</required>  
  13.             <fragment>true</fragment>  
  14.         </attribute>  
  15.     </tag>  


测试页面: 
Java代码   收藏代码
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
  2.     pageEncoding="ISO-8859-1"%>  
  3. <%@ taglib uri="http://www.royzhou.com/myTagLib" prefix="myTag" %>  
  4. <%@ page import="java.util.*" %>  
  5. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  6. <html>  
  7. <head>  
  8. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  9. <title>Insert title here</title>  
  10. </head>  
  11. <body>  
  12. <%  
  13.     List list = new ArrayList();  
  14.     list.add("1");  
  15.     list.add("2");  
  16.     list.add("3");  
  17.     list.add("4");  
  18.     list.add("5");  
  19.     request.setAttribute("list",list);  
  20. %>  
  21. <myTag:HelloWorld userName="royzhou" />  
  22. <table>  
  23. <myTag:Iterator item="item" collection="list">  
  24.     <tr>  
  25.         <td>${item }</td>  
  26.     </tr>  
  27. </myTag:Iterator>  
  28. </table>  
  29. </body>  
  30. </html>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值