jsp标签编程『下』---开发迭代标签

本文介绍了一个用于JSP页面的自定义迭代标签IterateTag,该标签能够遍历存储在不同作用域内的List集合,并将每次迭代的对象暴露给JSP页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发一个迭代标签,此标签只可以对list进行迭代(学习)。

IterateTag.java:

package com.keith.tag;

import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

public class IterateTag extends TagSupport {
	// 属性名称
	private String name;
	// 属性保存范围
	private String scope;
	// 每次迭代的对象
	private String id;
	// 所以接受到的数据
	private Iterator<?> iter;

	@Override
	public int doStartTag() throws JspException {
		Object value = null;
		// 是否是page范围
		if ("page".equals(this.scope)) {
			value = super.pageContext
					.getAttribute(name, PageContext.PAGE_SCOPE);
		} else if ("request".equals(this.scope)) {
			// 是否是request范围
			value = super.pageContext.getAttribute(name,
					PageContext.REQUEST_SCOPE);
		} else if ("session".equals(this.scope)) {
			// 是否是session范围
			value = super.pageContext.getAttribute(name,
					PageContext.SESSION_SCOPE);
		} else {
			// 是否是application范围
			value = super.pageContext.getAttribute(name,
					PageContext.APPLICATION_SCOPE);
		}

		//如果是List接口的实例
		if (value != null && value instanceof List<?>) {
			//像List接口进行向下转型
			this.iter = ((List<?>) value).iterator();
			if (iter.hasNext()) {
				super.pageContext.setAttribute(id, iter.next());
				//执行标签体内容
				return TagSupport.EVAL_BODY_INCLUDE; 
			}else{
				//退出标签执行
				return TagSupport.SKIP_BODY;
			}
		} else {
			//不是List接口实例,不处理
			//退出标签执行
			return TagSupport.SKIP_BODY;
		}
	}

	
	@Override
	public int doAfterBody() throws JspException {
		//判断是否还有内容
		if (iter.hasNext()) {
			super.pageContext.setAttribute(id, iter.next());
			//重复执行标签体
			return TagSupport.EVAL_BODY_AGAIN;
		}else{
			//退出标签执行
			return TagSupport.SKIP_BODY;
		}
	}


	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Iterator<?> getIter() {
		return iter;
	}

	public void setIter(Iterator<?> iter) {
		this.iter = iter;
	}

}

 iteratetag.tld:

<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">
    <!-- 标签库的版本 -->
	<tlib-version>1.0</tlib-version>
	<!-- 为标签苦在TLD中的描述名称 -->
	<short-name>iteratetag</short-name>
	<tag>
	<!-- 表示标签在JSP中的使用名称 -->
 		<name>iterate</name>
 		<!-- 表示这个标签所这项的Class -->
 		<tag-class>com.keith.tag.IterateTag</tag-class>
		<!-- 标签体内容为空 -->
 		<body-content>JSP</body-content>
 		<attribute>
 		<!-- format为属性名 -->
 			<name>id</name>
 			<!-- 表示此值必须设置 -->
 			<required>true</required>
 			<!-- 表示属性值是请求时表达式的结果 -->
 			<rtexprvalue>true</rtexprvalue>
 		</attribute>

 		<attribute>
 		<!-- format为属性名 -->
 			<name>name</name>
 			<!-- 表示此值必须设置 -->
 			<required>true</required>
 			<!-- 表示属性值是请求时表达式的结果 -->
 			<rtexprvalue>true</rtexprvalue>
 		</attribute>
 		
 		<attribute>
 		<!-- format为属性名 -->
 			<name>scope</name>
 			<!-- 表示此值必须设置 -->
 			<required>true</required>
 			<!-- 表示属性值是请求时表达式的结果 -->
 			<rtexprvalue>true</rtexprvalue>
 		</attribute>
	</tag>
</taglib>

 web.xml:

  <taglib>
  		<taglib-uri>iterate</taglib-uri>
  		<taglib-location>/WEB-INF/iteratetag.tld</taglib-location>
  </taglib>

 index.jsp:

<%@ page import="java.util.*" %>
<%@ taglib prefix="iteratetag" uri="iterate"%>

  <body>
	<%
		List<String> all = new ArrayList<String>();
		all.add("java");
		all.add("linux");
		all.add("C");
		request.setAttribute("all",all);
	 %>
	<iteratetag:iterate name="all" scope="request" id="url">
		${url }<br />
	</iteratetag:iterate>
  </body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值