现在的位置: 首页 > 综合 > 正文

自定义JSP中的Taglib标签之五自定义标签之SimpleTagSupport使用

2014年02月14日 ⁄ 综合 ⁄ 共 3356字 ⁄ 字号 评论关闭

Simple的生命周期:

1.在jsp中使用这个标签的实例后,调用setJspContext()

2.调用setParent():这个方法是在有标签嵌套的情况下调用

3.setters

4.setJspBody():如果标签体有body,调用此方法

5.doTag():所有的标签逻辑,业务计算,迭代都在这里实现

6.doTag() return的时候锁定属性值

和1TagSupport.2BodyTagSupport的区别:

没有doS,doE方法

doTag()只调用一次

没有标签池,每次再用的话需要从新创建

在body中不能有表达式

API:

SimpleTag接口和SimpleTagSupport实现类

引例1:

程序目标:有if功能的自定义标签

知识点:

1.JspFragment对象,原始body

2.getJspBody()将得到body

3.invoke(null):body的方法,执行body

4.tld的写法:在<body-content>中写scriptless

java类:

package yuchen.ctab.simpleTag.s1;

import java.io.IOException;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.*;

public class SimpleIf extends SimpleTagSupport{

private boolean cond;

public void setCond(boolean cond) {

this.cond = cond;

}

@Override

public void doTag() throws JspException, IOException {

// TODO Auto-generated method stub

JspFragment body=getJspBody();//原始body

if(this.cond){

getJspBody().invoke(null);//执行body

}

}

}

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>1.0</tlib-version>

<jsp-version>2.0</jsp-version>

<short-name>simpleif</short-name>

<uri>/mysimpleif</uri>

<tag>

<name>if</name>

<tag-class>yuchen.ctab.simpleTag.s1.SimpleIf</tag-class>

<body-content>scriptless</body-content>

<attribute>

<name>cond</name>

<required>true</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

</tag>

</taglib>

jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<%@taglib prefix="mysimpleif" uri="/mysimpleif" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<mysimpleif:if cond="true">

hello world!

</mysimpleif:if>

<mysimpleif:if cond="false">

hello world!

</mysimpleif:if>

</body>

</html>

引例2:

带有while循环功能的标签

知识点:

doTag方法只执行一次

invoke(null)null为默认的输出流,相当于getJspContext().getOut()

java类:

package yuchen.ctab.simpleTag.s2;

import java.io.IOException;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.*;

public class SimpleWhile extends SimpleTagSupport{

private int counts;

public void setCounts(int counts) {

this.counts = counts;

}

@Override

public void doTag() throws JspException, IOException {

// TODO Auto-generated method stub

JspFragment body=getJspBody();

for(int i=0;i<this.counts;i++){

body.invoke(null);//null为默认的输出流,相当于

//getJspContext().getOut()

}

}

}

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>1.0</tlib-version>

<jsp-version>2.0</jsp-version>

<short-name>simplewhile</short-name>

<uri>/mysimplewhile</uri>

<tag>

<name>loop</name>

<tag-class>yuchen.ctab.simpleTag.s2.SimpleWhile</tag-class>

<body-content>scriptless</body-content>

<attribute>

<name>counts</name>

<required>true</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

</tag>

</taglib>

jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<%@taglib prefix="mysimplewhile" uri="/mysimplewhile" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<mysimplewhile:loop counts="5">

hello world<br>

</mysimplewhile:loop>

</body>

</html>

抱歉!评论已关闭.