自定义JSP中的Taglib标签之五自定义标签之SimpleTagSupport使用
2011-11-08 13:19
573 查看
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>
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>
相关文章推荐
- [教程]自定义JSP中的Taglib标签之五自定义标签之SimpleTagSupport使用
- 自定义JSP中的Taglib标签之五自定义标签之SimpleTagSupport使用
- jsp 自定义标签-SimpleTagSupport 使用笔记
- 利用 JSP 2 提供的 SimpleTagSupport 开发自定义标签
- 利用 JSP 2 提供的 SimpleTagSupport 开发自定义标签
- 利用 JSP 2 提供的 SimpleTagSupport 开发自定义标签
- JSP 自定义标签 taglib TagSupport BodyTagSupport
- 利用 JSP 2 提供的 SimpleTagSupport 开发自定义标签
- 用 JSP 2 提供的 SimpleTagSupport 开发自定义标签
- Java创建自定义标签SimpleTagSupport并在页面中使用
- 利用 JSP 2 提供的 SimpleTagSupport 开发自定义标签
- JSP 2.x用SimpleTag开发自定义标签的方法
- JSTL基础 SimpleTagSupport 自定义标签 scriptless时,抛出空指针异常的示例
- 通过servlet.jsp.tagext.TagSupport的自定义标签实现按钮权限控制
- 自定义jsp标签: TagSupport与BodyTagSupport的区别 (转)
- 利用SimpleTagSupport接口自定义标签
- 自定义jsp标签: TagSupport与BodyTagSupport的区别
- 自定义jsp标签: TagSupport与BodyTagSupport的区别
- JSP中的Simple自定义标签使用
- JSTL基础 SimpleTagSupport 自定义标签 标签体empty