Iterate.java#
package customTag;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class Iterate extends TagSupport{
private Object items;
private String var;
private Iterator it;
public Object getItems() {
return items;
}
public void setItems(Object items) {
this.items = items;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
@Override
public int doAfterBody() throws JspException {
if(it.hasNext()) {
this.pageContext.setAttribute(var, it.next());
return EVAL_BODY_AGAIN;
}else {
return SKIP_BODY;
}
}
@Override
public int doStartTag() throws JspException {
if(items == null || !(items instanceof Collection)) {
return SKIP_BODY;
}
it = ((Collection)items).iterator();
if(it.hasNext()) {
this.pageContext.setAttribute(var, it.next());
return EVAL_BODY_INCLUDE;
}else {
return SKIP_BODY;
}
}
}
TagDemo2.java(小写转大写)#
package customTag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class TagDemo2 extends BodyTagSupport{
private int count = 1;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public int doAfterBody() throws JspException {
if(count > 0) {
String bodyString = getBodyContent().getString();
try {
if(bodyString != null) {
JspWriter out = getPreviousOut();
out.print(bodyString.toUpperCase());
}
bodyContent.clearBody();
} catch (IOException e) {
e.printStackTrace();
}
count--;
return EVAL_BODY_AGAIN;
}else {
count = 1;
return SKIP_BODY;
}
}
}
MySimpleTag.java#
package customTag;
import java.io.IOException;
import java.io.StringWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class MySimpleTag extends SimpleTagSupport{
private int count = 1;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public void doTag() throws JspException, IOException {
JspWriter out = this.getJspContext().getOut();
String str = invokeBody();
for(int i = 0;i<count;i++) {
out.println(str.toUpperCase());
}
}
protected String invokeBody() throws JspException{
JspFragment body = getJspBody();
StringWriter sw = new StringWriter();
try {
if(body != null) {
body.invoke(sw);
}
} catch (JspException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sw.toString();
}
}
iterate.tld#
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd" >
<taglib>
<tlib-version>tlib-version</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>short-name</short-name>
<uri>http://www.hzc.com/tag/iterate</uri>
<tag>
<name>iterate</name>
<tag-class>customTag.Iterate</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>toUpperCase</name>
<tag-class>customTag.TagDemo2</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>count</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>SimpleUpperCase</name>
<tag-class>customTag.MySimpleTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>count</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
web.xml#
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>jspdemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>http://www.hzc.com/tag/iterate</taglib-uri>
<taglib-location>/WEB-INF/tlds/iterate.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://www.hzc.com/ELFunction</taglib-uri>
<taglib-location>/WEB-INF/tlds/ELFunction.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
iterate.jsp#
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://www.hzc.com/tag/iterate" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<String> list = new ArrayList<String>();
list.add("hzc");
list.add("zzl");
pageContext.setAttribute("list", list);
%>
<h2>迭代</h2>
<c:iterate items="${list }" var="item">${item }<br></c:iterate>
<c:toUpperCase count="3">hzc<br></c:toUpperCase>
<c:SimpleUpperCase count="4">zzl<br></c:SimpleUpperCase>
</body>
</html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理