jsp自定义标签晋级【实现循环输出】

jsp的循环输出可以将我们的 网页内容循环输出到web页面,实现代码如下:

 

Jsp代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.c3g.com" prefix="c3g"%>
<%
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 'Demo1.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>
    This is my JSP page. <br/>
    <c3g:myLoop times="5"><!-- times 代码的作用为控制myLoop 代码调用次数,也就是可以直接在jsp页面控制循环次数 -->
    	我被输出啦!!!哥们!<br/>
	</c3g:myLoop>   
	<br/> 
  </body>
</html>


LoopOutTag 类:

package cn.csdn.web.tag;

import java.io.IOException;
import java.util.Date;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.SimpleTagSupport;

public class LoopOutTag extends SimpleTagSupport{
	//
	private int times;
	private Date date;
	//建立set方法
	public void setTimes(int times) {
		this.times = times;
	}
	
	public void setDate(Date date) {
		this.date = date;
	}

	public void doTag() throws JspException, IOException {
		
		for(int i =0;i<=times;i++){
			this.getJspBody().invoke(null);
		}
		
	}
	

}

标签库文件 c3g.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">
    
    <description>A tag library exercising SimpleTag handlers.</description>
    <tlib-version>1.0</tlib-version>
    <short-name>c3g</short-name>
    <uri>http://www.c3g.com</uri>
   
    <tag>
        <description>Outputs Hello, World</description>
        <name>myLoop</name>
        <tag-class>cn.csdn.web.tag.LoopOutTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute><!--用于设置标签的属性-->
        	<name>times</name><!--属性值,也就是jsp 里面的times参数-->
        	<required>true</required><!--指定该属性是不是必须有的 默认为 false-->
        	<rtexprvalue>false</rtexprvalue><!--指定属性值是不是可以在jsp执行时动态产生-->
        </attribute>
    </tag>

    </taglib>


这样就可以输出了!

输出结果就是

 

This is my JSP page

我被输出啦!!!哥们!

我被输出啦!!!哥们!

我被输出啦!!!哥们!

我被输出啦!!!哥们!

我被输出啦!!!哥们!

 

Power By Jackie Bai

你可能感兴趣的:(jsp自定义标签晋级【实现循环输出】)