Tip:invoke方法详解 及实现循环的源代码
l JspFragment.invoke方法可以说是JspFragment最重要的方法,利用这个方法可以控制是否执行和输出标签体的内容、是否迭代执行标签体的内容或对标签体的执行结果进行修改后再输出。例如:
• 在标签处理器中如果没有调用JspFragment.invoke方法,其结果就相当于忽略标签体内容;
• 在标签处理器中重复调用JspFragment.invoke方法,则标签体内容将会被重复执行;
• 若想在标签处理器中修改标签体内容,只需在调用invoke方法时指定一个可取出结果数据的输出流对象(例如StringWriter),让标签体的执行结果输出到该输出流对象中,然后从该输出流对象中取出数据进行修改后再输出到目标设备,即可达到修改标签体的目的。
开发带属性的标签
l 自定义标签可以定义一个或多个属性,这样,在JSP页面中应用自定义标签时就可以设置这些属性的值,通过这些属性为标签处理器传递参数信息,从而提高标签的灵活性和复用性。
l 要想让一个自定义标签具有属性,通常需要完成两个任务:
l 在标签处理器中编写每个属性对应的setter方法
l 在TLD文件中描术标签的属性
l 为自定义标签定义属性时,每个属性都必须按照JavaBean的属性命名方式,在标签处理器中定义属性名对应的setter方法,用来接收JSP页面调用自定义标签时传递进来的属性值。 例如属性url,在标签处理器类中就要定义相应的setUrl(String url)方法。
l 在标签处理器中定义相应的set方法后,JSP引擎在解析执行开始标签前,也就是调用doStartTag方法前,会调用set属性方法,为标签设置属性。
在TLD中描述标签属性
元素名 | 是否必须指定 | 描 述 |
description | 否 | 用于指定属性的描述信息。 |
name | 是 | 用于指定属性的名称。属性名称是大小写敏感的,并且不能以jsp、_jsp、java和sun开头。 |
required | 否 | 用于指定在JSP页面中调用自定义标签时是否必须设置这个属性。其取值包括true和false,默认值为false,true表示必须设置,否则可以设置也可以不设置该属性。 |
rtexprvalue | 否 | rtexprvalue是runtime expression value(运行时表达式)的英文简写,用于指定属性值是一个静态值或动态值。其取值包括true和false,默认值为false,false表示只能为该属性指定静态文本值,例如"123";true表示可以为该属性指定一个JSP动态元素,动态元素的结果作为属性值,例如JSP表达式<%=value %>。 |
type | 否 | 用于指定属性值的Java类型。 |
用标签实现循环功能---标签开发之三(Inber原作)
主要练习知识点:页面与脚本变量关键字:页面与脚本变量,标签
第一步:标签处理程序:ForTag.java
package net.inber.jsptaglibs.iteration;
import jav...
用标签实现循环功能---标签开发之三(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
<>
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">
A tag library exercising SimpleTag handlers.
1.0
examples
/demotag
A simple tab library for the examples
bookView
net.inber.jsptaglibs.iteration.ForTag
jsp
index
java.lang.Integer
true
jsp
from
true
true
to
true
true
(3)创建JSP文件index.jsp
loops
(4)布署tomcat web.xml
<>
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">
Welcome to Tomcat
Welcome to Tomcat
/tagURI
/WEB-INF/tlds/configTLD.tld
好了下面该你动手了,源文件下载地址:http://inber.51.net/csdn_blog/soft/fortag.rar
如有问题忙快和我联系。
<!-- 导入标签库,指定mytag前缀的标签,
由http://www.crazyit.org/mytaglib的标签库处理 -->
<%@ taglib uri="http://www.crazyit.org/mytaglib" prefix="mytag"% >
...
<!-- 其他HTML内容 -->
<!-- 使用标签 ,其中mytag是标签前缀,根据taglib的编译指令,
mytag前缀将由http://www.crazyit.org/mytaglib的标签库处理 -- >
<mytag:query
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javaee"
user="root"
pass="32147"
sql="select * from newsinf"/>
本文来自:http://www.bianceng.cn/webkf/JSP/201011/20450_6.htm
public class IteratorTag extends SimpleTagSupport
{
//标签属性,用于指定需要被迭代的集合
private String collection;
//标签属性,指定迭代集合元素,为集合元素指定的名称
private String item;
//collection属性的setter和getter方法
public void setCollection(String collection)
{
this.collection = collection;
}
public String getCollection()
{
return this.collection;
}
//item属性的setter和getter方法
public void setItem(String item)
{
this.item = item;
}
public String getItem()
{
return this.item;
}
//标签的处理方法,简单标签处理类只需要重写doTag方法
public void doTag() throws JspException, IOException
{
//从page scope中获取属性名为collection的集合
Collection itemList = (Collection)getJspContext().
getAttribute(collection);
//遍历集合
for (Object s : itemList)
{
//将集合的元素设置到page 范围
getJspContext().setAttribute(item, s );
//输出标签体
getJspBody().invoke(null);
}
}
}
本文来自:http://www.bianceng.cn/webkf/JSP/201011/20450_6.htm