javaweb之自定义标签库——简单的foreach标签(对List迭代)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/example" prefix="c" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>开发迭代标签(foreach)(重复执行标签体案例)</title>
  </head>
  
  <body>
  	<%--模拟servlet的代码 --%>
  	<%
  		List<String> list = new ArrayList<String>();
  		list.add("aaa");
  		list.add("bbb");
  		list.add("ccc");
  		request.setAttribute("list", list);
  	%>
  	
  	<c:foreach var="str" items="${list }">
  		${str}<br/>
  	</c:foreach>
  </body>
</html>
package cn.itcast.web.tag.example;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
//开发迭代标签(foreach)
public class ForeachTag extends SimpleTagSupport {
	//因为传进来的可能是List也可能是Map也可能是数组,所以用Object类型
	private Object items;
	//不知道集合中存的是什么,但是var的类型要是String因为他是迭代的关键字,存入pageContext中的key
	private String var;
	
	public void setItems(Object items) {
		this.items = items;
	}
	public void setVar(String var) {
		this.var = var;
	}
	
	@Override
	public void doTag() throws JspException, IOException {
		//迭代标签
		List list = (List) items;
		Iterator it =  list.iterator();
		while(it.hasNext()) {
			//不知道List中存的是什么东西,所以要用Object
			Object value = it.next();
			//取到一个东西就把它存入pageContext中,以var为关键字来存
			this.getJspContext().setAttribute(var, value);
			//存入域中就通知标签体执行
			//执行标签体就会把存入的东西拿走!
			this.getJspBody().invoke(null);
			//下次循环就会把上次存的东西覆盖,再拿出来
			
			/*
			 	这两行代码特别巧可以在页面中用EL表达式拿到循环的值
				this.getJspContext().setAttribute(var, value);
				this.getJspBody().invoke(null);
			*/
		}
	}
	
}

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
    <description>A tag library exercising SimpleTag handlers.</description>
    <tlib-version>1.0</tlib-version>
    <short-name>example</short-name><!-- 描述文件的名称 -->
    <uri>/example</uri><!-- 描述文件的uri -->
    
    
    <tag>
        <name>referer</name>
        <tag-class>cn.itcast.web.tag.example.RefererTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
        	<name>site</name>
        	<required>true</required>
        	<rtexprvalue>true</rtexprvalue>
        </attribute>
        
        <attribute>
        	<name>page</name>
        	<required>true</required>
        	<rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    
    <tag>
        <name>if</name>
        <tag-class>cn.itcast.web.tag.example.IfTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
        	<name>test</name>
        	<required>true</required>
        	<rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    
    <tag>
        <name>choose</name>
        <tag-class>cn.itcast.web.tag.example.ChooseTag</tag-class>
        <body-content>scriptless</body-content>
    </tag>
    
    <tag>
        <name>when</name>
        <tag-class>cn.itcast.web.tag.example.WhenTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
        	<name>test</name>
        	<required>true</required>
        	<rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    
     <tag>
        <name>otherwise</name>
        <tag-class>cn.itcast.web.tag.example.OtherWiseTag</tag-class>
        <body-content>scriptless</body-content>
    </tag>
    
    <tag>
        <name>foreach</name>
        <tag-class>cn.itcast.web.tag.example.ForeachTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
        	<name>items</name>
        	<required>true</required>
        	<rtexprvalue>true</rtexprvalue>
        </attribute>
        
         <attribute>
        	<name>var</name>
        	<required>true</required>
        	<rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>
   
</taglib>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值