教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Jsp  >  正文 jsp自定义标签ifelse与遍历自定义标签

jsp自定义标签ifelse与遍历自定义标签

发布时间:2015-09-28   编辑:jiaochengji.com
本文介绍了jsp自定义标签之ifelse与遍历自定义标签的用法,感兴趣的朋友参考下。

第一个例子:
简单的jsp自定标签获取内容:
首先创建一个jsp实例类然后继承SimpleTagSupport类
然后实现父类的doTag()方法
在这个方法里获取标签体里的内容this.getJspBody();
返回的是JspFragment 类,根据这个类对象调用invoke(this.getJspContext().getOut());
这个方法里面也可以写空,所表达的意思也是输出到浏览器;
代码:
 

复制代码 代码示例:
public class SimpleDmeo1 extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
JspFragment js =this.getJspBody();
js.invoke(null);
}
}
 

然后,在写tld文件标签库描述文件,和jsp文件,这些都较为简单
如果不想执行某个内容就抛出异常
throw new skipPageException();和面内容就不会显示
接下来是一个带属性的jsp自定义标签文件
代码:
 

复制代码 代码示例:
public class SimpleDmeo1 extends SimpleTagSupport {
private int counts;
public void setCounts(int counts) {
this.counts = counts;
}
@Override
public void doTag() throws JspException, IOException {
JspFragment js =this.getJspBody();
for(int i=0;i<counts;i++){ //循环获取
js.invoke(null);
}
}
 
}
<description>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>c</short-name>前缀名
<uri>http://www.csdn.com</uri>
<tag>
<name>demo</name>
<tag-class>com.csdn.simple.SimpleDmeo1</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>counts</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>

然后在jsp文件 中写出内容;
代码:
 

复制代码 代码示例:
<hbsi:demo counts="3">aaaaaaa<br/></hbsi:demo> //输出三编
JspFragment js = this.getJspBody();
StringWriter jw = new StringWriter();
js.invoke(jw);
String s = jw.toString().toUpperCase();
JspWriter out =this.getJspContext().getOut();
for(int i=0;i<counts2;i++){
out.print(s);
}
}
 

这是转成大写的代码,其他的都一致。

您可能感兴趣的文章:
jsp自定义标签ifelse与遍历自定义标签
jsp自定义标签foreach遍历与转义字符
JSP常见问题
html5 command标签的用法和<command>标签的使用案例详解
HTML5新增标签使用方法
html5 header标签怎么用?html5 header标签的作用介绍
php在jsp里面使用的么
html5 header标签是什么意思?html5 header标签的用法详解(附实例)
html5页面结构的变化以及增加和删除标签的总结
html5新增加的标签有哪些

关键词: 遍历   
[关闭]
~ ~