用标签实现循环功能---标签开发之三(Inber原作)
2004-08-26 09:30
411 查看
用标签实现循环功能---标签开发之三(Inber原作)
主要练习知识点:页面与脚本变量
关键字:页面与脚本变量,标签
第一步:标签处理程序:ForTag.java
package net.inber.jsptaglibs.iteration;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/**
* Given a range of values, this tag iterates over each value and makes it
* available to the page.
*
* @author Inber Gong
*/
public class ForTag extends TagSupport {
/** the starting index (defaults to zero) */
private int from = 0;
/** the finishing index */
private int to;
/** the current index */
private int index;
/**
* Called when the starting tag is encountered.
*/
public int doStartTag() throws JspException {
// setup the counter variable
index = from;
if (index < to) {
// put the first index into the page
pageContext.setAttribute("index", new Integer(index));
// and include the body
return EVAL_BODY_INCLUDE;
} else {
// there are no elements so skip the body
return SKIP_BODY;
}
}
/**
* Called after the body has been included.
*/
public int doAfterBody() throws JspException {
// increment the counter
index++;
if (index < to) {
// put the next index into the page
pageContext.setAttribute("index", new Integer(index));
// and instruct the JSP engine to re-evaluate the body of this tag
return EVAL_BODY_AGAIN;
} else {
// there are no more elements so skip the body
return SKIP_BODY;
}
}
/**
* Setter for the "from" attribute.
*
* @param i an integer value
*/
public void setFrom(int i) {
this.from = i;
}
/**
* Setter for the "to" attribute.
*
* @param i an integer value
*/
public void setTo(int i) {
this.to = i;
}
}
(2)配置标签库描述符(TLD)文件 configTLD.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<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 web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>examples</short-name>
<uri>/demotag</uri>
<description>
A simple tab library for the examples
</description>
<tag>
<description></description>
<name>bookView</name>
<tag-class>net.inber.jsptaglibs.iteration.ForTag</tag-class>
<body-content>jsp</body-content>
<!-- 声明脚本变量 start-->
<variable>
<name-given>index</name-given>
<variable-class>java.lang.Integer</variable-class>
<declare>true</declare>
</variable>
<body-content>jsp</body-content>
<!-- 声明脚本变量 end-->
<!-- 设置属性 start-->
<attribute>
<name>from</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>to</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 设置属性 end-->
</tag>
</taglib>
(3)创建JSP文件index.jsp
<html>
<%@ taglib uri="/tagURI" prefix="fromTo" %>
<body>
<h1>loops</h1>
<fromTo:bookView from="0" to="10">
<p>
<br>
<%=index.intValue()%>
</p>
</fromTo:bookView>
</body>
</html>
(4)布署tomcat web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Copyright 2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app 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
-app_2_4.xsd"
version="2.4">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<!-- JSPC servlet mappings start -->
<taglib>
<taglib-uri>/tagURI</taglib-uri>
<taglib-location>/WEB-INF/tlds/configTLD.tld</taglib-location>
</taglib>
<!-- JSPC servlet mappings end -->
</web-app>
好了下面该你动手了,源文件下载地址:http://inber.51.net/csdn_blog/soft/fortag.rar
如有问题忙快和我联系。
主要练习知识点:页面与脚本变量
关键字:页面与脚本变量,标签
第一步:标签处理程序:ForTag.java
package net.inber.jsptaglibs.iteration;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/**
* Given a range of values, this tag iterates over each value and makes it
* available to the page.
*
* @author Inber Gong
*/
public class ForTag extends TagSupport {
/** the starting index (defaults to zero) */
private int from = 0;
/** the finishing index */
private int to;
/** the current index */
private int index;
/**
* Called when the starting tag is encountered.
*/
public int doStartTag() throws JspException {
// setup the counter variable
index = from;
if (index < to) {
// put the first index into the page
pageContext.setAttribute("index", new Integer(index));
// and include the body
return EVAL_BODY_INCLUDE;
} else {
// there are no elements so skip the body
return SKIP_BODY;
}
}
/**
* Called after the body has been included.
*/
public int doAfterBody() throws JspException {
// increment the counter
index++;
if (index < to) {
// put the next index into the page
pageContext.setAttribute("index", new Integer(index));
// and instruct the JSP engine to re-evaluate the body of this tag
return EVAL_BODY_AGAIN;
} else {
// there are no more elements so skip the body
return SKIP_BODY;
}
}
/**
* Setter for the "from" attribute.
*
* @param i an integer value
*/
public void setFrom(int i) {
this.from = i;
}
/**
* Setter for the "to" attribute.
*
* @param i an integer value
*/
public void setTo(int i) {
this.to = i;
}
}
(2)配置标签库描述符(TLD)文件 configTLD.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<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 web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>examples</short-name>
<uri>/demotag</uri>
<description>
A simple tab library for the examples
</description>
<tag>
<description></description>
<name>bookView</name>
<tag-class>net.inber.jsptaglibs.iteration.ForTag</tag-class>
<body-content>jsp</body-content>
<!-- 声明脚本变量 start-->
<variable>
<name-given>index</name-given>
<variable-class>java.lang.Integer</variable-class>
<declare>true</declare>
</variable>
<body-content>jsp</body-content>
<!-- 声明脚本变量 end-->
<!-- 设置属性 start-->
<attribute>
<name>from</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>to</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 设置属性 end-->
</tag>
</taglib>
(3)创建JSP文件index.jsp
<html>
<%@ taglib uri="/tagURI" prefix="fromTo" %>
<body>
<h1>loops</h1>
<fromTo:bookView from="0" to="10">
<p>
<br>
<%=index.intValue()%>
</p>
</fromTo:bookView>
</body>
</html>
(4)布署tomcat web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Copyright 2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app 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
-app_2_4.xsd"
version="2.4">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<!-- JSPC servlet mappings start -->
<taglib>
<taglib-uri>/tagURI</taglib-uri>
<taglib-location>/WEB-INF/tlds/configTLD.tld</taglib-location>
</taglib>
<!-- JSPC servlet mappings end -->
</web-app>
好了下面该你动手了,源文件下载地址:http://inber.51.net/csdn_blog/soft/fortag.rar
如有问题忙快和我联系。
相关文章推荐
- iOS开发:一个瀑布流的设计与实现(已实现缓存池功能,该功能使得瀑布流cell可以循环利用)
- 自定义标签的开发及使用自定义标签实现迭代foreach循环
- Android开发实现广告无限循环功能示例
- iOS开发:一个瀑布流的设计与实现(已实现缓存池功能,该功能使得瀑布流cell可以循环利用)
- 自定义标签的开发及使用自定义标签实现迭代foreach循环
- 自定义标签的开发及使用自定义标签实现迭代foreach循环
- 微信小程序开发之radio实现显示和隐藏功能 原创 2017年07月04日 20:33:07 标签: 前端开发 / 微信 我们在开发微信小程序的时候,经常会用到显示和隐藏,
- 深入体验JavaWeb开发内幕——使用简单标签实现传统标签开发实现的几个功能
- IOS开发实现 UIScrollView 的循环轮播功能
- C++ Builder XE8安卓开发之实现左右滑屏切换标签(Tab)功能
- 自定义标签的开发及使用自定义标签实现迭代foreach循环
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载][续]
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载续][重点推荐控件]
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载][续]
- J2EE 0.7.1—报表功能。传入封装数据的list实现打印标签报表。。
- 我在开发过程总结的一套实现常用功能的函数
- 软件开发中几个常用功能的实现
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]