自定义标签文件(二)

开发自定义标签,实现标准标签c:foreach相同的功能。以下是关键代码:

 

实现迭代的标签处理类ForeachTag.java

 1 package com.abc;
 2 
 3 import java.io.IOException;
 4 import java.lang.reflect.Array;
 5 import java.util.*;
 6 
 7 import javax.servlet.jsp.JspException;
 8 import javax.servlet.jsp.tagext.SimpleTagSupport;
 9 
10 
11 public class ForeachTag extends SimpleTagSupport {
12     
13     //对应标签元素var
14     private String var;
15     //对应标签元素items
16     private Object items;
17     
18     // 使用Collection接口对象
19     private Collection<Object> collecion;
20     
21     public void setVar(String var) {
22         this.var = var;
23     }
24     
25     public void setItems(Object items) {
26         this.items = items;
27     }
28     
29     /**
30      * 判断参数类型后赋值给collection对象。
31      * 
32      * @param items
33      */
34     public void setCollecion(Object items) {
35 
36         // 如果是collection对象,在这可以看做是子类对象中的Set和List。
37         if (items instanceof Collection) {
38             
39             this.collecion = (Collection) items;
40             
41         }
42 
43         // 如果是一个Map对象
44         if (items instanceof Map) {
45             
46             Map map = (Map) items;
47             this.collecion = map.entrySet();
48         }
49         
50         //如果是一个数组(无视数组元素类型,可查阅Class和Array类)
51         if (items.getClass().isArray()){
52             //如果是数组,则创建实例,以便做迭代处理
53             this.collecion = new ArrayList();
54             //获取数组长度,可查阅Array类
55             int length = Array.getLength(items);
56             //循环遍历数组元素,并添加到collection对象中
57             for (int i = 0; i < length; i++) {
58                 Object o = Array.get(items, i);
59                 this.collecion.add(o);
60             }
61         }
62     }
63     
64     /**
65      * 覆盖doTag()方法,实现迭代
66      */
67     @Override
68     public void doTag() throws JspException, IOException {
69         
70         //调用set方法,设置collection对象
71         this.setCollecion(items);
72         
73         //获取collection的迭代器
74         Iterator<Object> iteratior = collecion.iterator();
75         
76         while (iteratior.hasNext()) {
77             Object value = iteratior.next();
78             //将对象添加到pageContext中,jspContext是pageContext的超类。
79             getJspContext().setAttribute(var, value);
80             //输出标签体内容
81             getJspBody().invoke(null);
82         }
83     }
84 
85 }

 标签库描述文件myTags.tld:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <taglib>
 3  <tlib-version>1.0</tlib-version>
 4  <jsp-version>2.0</jsp-version>
 5  <short-name>mine</short-name>
 6  <uri>http://myTags.com/myTags</uri>
 7  <tag>
 8      <description>测试</description>
 9      <name>out</name>
10      <tag-class>com.abc.FirstST</tag-class>
11      <body-content>scriptless</body-content>
12          <attribute>
13              <name>test</name>
14              <requird>true</requird>
15              <rtexprvalue>true</rtexprvalue>
16          </attribute>
17  </tag>
18  <tag>
19      <description>Foreach遍历输出</description>
20      <name>foreach</name>
21      <tag-class>com.abc.ForeachTag</tag-class>
22      <body-content>scriptless</body-content>
23          <attribute>
24              <name>var</name>
25              <requird>true</requird>
26              <rtexprvalue>true</rtexprvalue>
27          </attribute>
28          <attribute>
29              <name>items</name>
30              <requird>true</requird>
31              <rtexprvalue>true</rtexprvalue>
32          </attribute>
33  </tag>
34 </taglib>


测试页面代码index.jsp :

 1 <%@ taglib prefix="myTags" tagdir="/WEB-INF/tags"%>
 2 <%@ taglib prefix="mine" uri="/WEB-INF/tags/myTags.tld" %>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"  %>
 4 <html>
 5   <head>
 6    <title>测试自定义标签</title>
 7   </head>
 8   <body>
 9     <% Integer num[] = {1,2,3}; 
10     request.setAttribute("num", num);%>
11     <% Map map = new LinkedHashMap();
12         map.put("a", "aaa");
13         map.put("b","bbb");
14         map.put("c", "ccc");
15         request.setAttribute("map", map);
16      %>
17      <br>---------自定义标签mine:foreach迭代效果---------<br>
18       <mine:foreach var="object" items="${num}">
19           ${object}
20       </mine:foreach>
21        <mine:foreach var="object" items="${map}">
22           ${object}
23       </mine:foreach>
24        <br>---------标准标签c:foreach迭代效果---------<br>
25        <c:forEach var="object" items="${num}">
26            ${object}
27        </c:forEach>
28        <c:forEach var="object" items="${map}">
29            ${object}
30        </c:forEach>
31   </body>
32 </html>


测试效果:

 

 

最后,感谢方立勋老师,《30天轻松掌握JavaWeb编程》视频系列给予了我很大帮助。

posted @ 2015-05-09 23:49  易枫  阅读(316)  评论(0编辑  收藏  举报