JSP自定义标签(3)
2011-11-17 19:17
387 查看
八、SimpleTagSupport类
在JSP2.0后,为了简化标签开发的难度,就可以使用SimpleTagSupport进行开发;1.开发一般标签
注意点:(1)需要继承SimpleTagSupport类;
(2)实现public void doTag()throws JspException,IOException;
(3)super.getJspContext().getOut().write("...."); 进行输出;
(4)在SimpleTagSupport中,tld中的<body-content>内容不能为JSP,如果标签体不为空,则只能为scriptless;
代码:
SimpleTagSupportDemo.java
package org.tagext; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class SimpleTagSupportDemo extends SimpleTagSupport{ private String name; public String getName(){ return name; } public void setName(String name){ this.name = name; } public void doTag()throws JspException,IOException{ super.getJspContext().getOut().write("<h3>"+name+"</h3>"); } }
2.开发迭代标签
通过super.getJspBody().invoke(null);能够执行标签体内容;SimpleTagSupportDemo.java
package org.tagext; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; import java.util.*; public class SimpleTagSupportDemo extends SimpleTagSupport{ private String name; private String id; public String getName(){ return name; } public void setName(String name){ this.name = name; } public String getId(){ return id; } public void setId(String id){ this.id = id; } public void doTag()throws JspException,IOException{ Object value = super.getJspContext().getAttribute(name,PageContext.PAGE_SCOPE); Iterator<String> iter = ((List<String>)value).iterator(); while(iter.hasNext()){ super.getJspContext().setAttribute(id,iter.next()); super.getJspBody().invoke(null); } } }
综合看来,SimpleTagSupport比起前面的TagSupport,BodyTagSupport,简单了许多,不需要任何返回值;
九、常见问题
1.区分是否有标签体<xiazdong:hello name="">
</xiazdong:hello>
是属于有标签体的,只是标签体为空;
<xiazdong:hello name=""/>为标签体为空;
相关文章推荐
- Jsp自定义标签_防盗链标签
- jsp自定义标签实现java的switch……case实例
- 使用自定义标签实现JSP页面和代码的分离
- JSP自定义标签-Select标签
- JSP自定义标签实现数据字典
- jsp自定义标签配置文件 *.tld
- JavaEE jsp自定义标签
- 自定义标签(JspFragment类、invoke方法、开发带属性的标签)
- jsp自定义标签
- jsp自定义标签
- jsp自定义标签(taglib)编写的原理和实现
- jsp使用自定义标签taglib分页系列——ContentBase.java
- JSP自定义标签_属性接收复杂类型处理方式
- jsp总结之jsp自定义标签
- Java程序员从笨鸟到菜鸟之(二十)jsp自定义标签
- JSP自定义标签
- jsp自定义标签详解(2)
- 掌握jsp自定义标签:(二)
- JSP自定义标签(三) 多选控件(2)
- jsp自定义标签及简单实例