jsp标签循环数字到jsp页面

本文介绍了如何通过自定义标签库(my4:myeach)来实现在JSP页面上动态展示指定范围内的整数序列,并结合实际案例详细解释了标签库的使用方法。

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

jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.cgmcc.com/jsp/myech" prefix="my4" %>


<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'myeach.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<%
for(int i=0;i<=9;i++){
request.setAttribute("mm",i);
%>
${mm}<br>
<%
}
%>

<hr>


<!-- 标签循环输出0-9 -->
<my4:myeach start="0" end="9" step="1" var="ss" flag="4">
${ss} ***<br/>

</my4:myeach>


</body>
</html>


java
package com.cgm.myeach;

import java.io.IOException;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTagSupport;


public class MyEach extends SimpleTagSupport {
//<my4:myeach start="0" end="9" step="1" var="ss"></my4:myeach>
private int start;
private int end;
private int step;
private int flag;

public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}

private String var;
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public int getStep() {
return step;
}
public void setStep(int step) {
this.step = step;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}

@Override
public void doTag() throws JspException, IOException {

for (int i = start; i <=end; i+=step) {

if(i==flag){
System.out.println(i);
this.getJspContext().setAttribute(var,i);
this.getJspBody().invoke(this.getJspContext().getOut()); //输出到浏览器

}
}


// this.getJspContext().getOut();
//this.getJspBody().invoke(null);

}





@Override
protected JspFragment getJspBody() {
// TODO Auto-generated method stub
return super.getJspBody();
}

@Override
protected JspContext getJspContext() {
// TODO Auto-generated method stub
return super.getJspContext();
}

@Override
public JspTag getParent() {
// TODO Auto-generated method stub
return super.getParent();
}

@Override
public void setJspBody(JspFragment jspBody) {
// TODO Auto-generated method stub
super.setJspBody(jspBody);
}

@Override
public void setJspContext(JspContext pc) {
// TODO Auto-generated method stub
super.setJspContext(pc);
}

@Override
public void setParent(JspTag parent) {
// TODO Auto-generated method stub
super.setParent(parent);
}



}


tld
<?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">
<tlib-version>1.0</tlib-version>
<short-name>my4</short-name>
<uri>http://www.cgmcc.com/jsp/myech</uri>
<tag>
<name>myeach</name>
<tag-class>com.cgm.myeach.MyEach</tag-class>
<body-content>scriptless</body-content>
<!--
<my4:myeach start="0" end="9" step="1" var="ss"></my4:myeach>
-->
<attribute>
<name>start</name>
<required>true</required>
</attribute>
<attribute>
<name>end</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>step</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

<attribute>
<name>flag</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>


</tag>
</taglib>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值