07---jsp标签编程04(迭代标签)
来源:互联网 发布:程序员学什么专业 编辑:程序博客网 时间:2024/06/05 02:15
在一个MVC设计模式之中,一个jsp文件中最好不要出现任何的scriptlet代码,因为这样会破坏程序的结构。
而且维护起来非常的麻烦,上一次已经演示了如何进行判断标签的开发,那么
这次来观察一下如何使用标签进行迭代操作的开发;
需要先开发一个迭代标签的处理操作类;
package tag.lid.iterator;
import java.io.*;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class IteratorTag extends TagSupport{
private String name;//接受属性的名称
private String scope;//接受属性的范围
private String id;//这个id用于保存集合中的每一个元素
private Iterator<?> iterator;
public int doStartTag() throws JspException{
Object value=null;
if("page".equals(this.scope)){//是否是page范围
value=super.pageContext.getAttribute(this.name,PageContext.PAGE_SCOPE);
}
if("request".equals(this.scope)){//是否是request范围
value=super.pageContext.getAttribute(this.name,PageContext.REQUEST_SCOPE);
}
if("session".equals(this.scope)){//是否是session范围
value=super.pageContext.getAttribute(this.name,PageContext.SESSION_SCOPE);
}
if("application".equals(this.scope)){//是否是application范围
value=super.pageContext.getAttribute(this.name,PageContext.APPLICATION_SCOPE);
}
if(value!=null && value instanceof List<?>){//此处一List集合为例学习
this.iterator=((List<?>)value).iterator();
if(iterator.hasNext()){
//将属性保存在page属性范围之中
super.pageContext.setAttribute(this.id,iterator.next());
return TagSupport.EVAL_BODY_INCLUDE;
}
else{
return TagSupport.SKIP_BODY;
}
}else{
return TagSupport.SKIP_BODY;
}
}
public int doAfterBody() throws JspException{
if(iterator.hasNext()){
//将属性保存在page属性范围之中
super.pageContext.setAttribute(this.id,iterator.next());
return TagSupport.EVAL_BODY_AGAIN;
}
else{
return TagSupport.SKIP_BODY;
}
}
public void setName(String name){
this.name=name;
}
public void setScope(String scope){
this.scope=scope;
}
public void setId(String id){
this.id=id;
}
public String getName(){
return this.name;
}
public String getScope(){
return this.scope;
}
public String getId(){
return this.id;
}
}
编译上面程序;
下面开始编写标签的描述文件;
<tag>
<name>iterator</name>
<tag-class>
tag.lid.iterator.IteratorTag
</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>scope</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>id</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
下面在jsp页面中测试以上的标签是否可以正常使用:
<%@ page contentType="text/html" pageEncoding="gbk"%>
<%@ page import="java.util.*"%>
<%@ taglib prefix="mytag" uri="lid"%>
<html>
<head><title>这是测试</title></head>
<body>
<%//此代码这是测试,实际中此部分应该有servlet传递的
List<String> all=new ArrayList<String>();
all.add("lid");
all.add("yuj");
all.add("family");
request.setAttribute("all",all);//将内容保存在标签执行
%>
<mytag:present name="all" scope="request"><!--迭代之前的判断标签(之前已写过,此处直接用)-->
<mytag:iterator id="url" name="all" scope="request">
<h3>姓名:${url}</h3>
</mytag:iterator>
</mytag:present>
</body>
</html>
此处的操作比较简单,主要是为了掩饰各个方法之间的操作,以及为了以后讲解Struts提供了更方便的原理操作;
到此为我我们解决了jsp中的三种代码:
·接收属性
·判断
·迭代输出
- 07---jsp标签编程04(迭代标签)
- JSP迭代标签
- JSP标签JSTL(3)--迭代操作
- jsp struts标签迭代各种数据
- 使用JSTL开发jsp自定义标签开发---迭代标签
- jsp自定义标签(5):开发迭代的标签
- JSP自定义标签开发Foreach迭代标签
- JSP:自定义标签之开发迭代标签
- jsp+struct标签实现分页(只有jsp代码),内用到迭代标签、if标签、bean
- 07---jsp标签编程01(认识标签)
- 07---jsp标签编程02(有属性的标签)
- 07---jsp标签编程03(判断标签)
- 07---jsp标签编程06(简单标签)
- JSP标签编程----自定义标签
- 自定义迭代标签
- STRUTS2迭代标签
- 自定义迭代标签
- 自定义迭代标签
- Redis基础学习笔记
- 菜鸟从零开始的第一个应用上线记(三)
- oracle海量数据加快创建索引速度
- JS笔记:检测客户端(引擎、浏览器、平台、操作系统、移动设备)
- php-Arrays 函数-array_combine-用两个数组的值作为新数组的键与值来新建一个数组
- 07---jsp标签编程04(迭代标签)
- 隐式声明与内建函数不兼容问题
- 关于有符号数、无符号数和数据类型的总结(草稿)
- 关于《创建组件**失败。错误消息为**未能加载文件或程序集**或它的某一个依赖。系统找不到指定文件。》的解决方案
- 解决S5pv210 adb push u-boot.bin /system 的失败问题
- Activity的四种launchMode
- 07---jsp标签编程05(BodyTagSupport)---以后补充
- 密码加密方式总结
- [转]理解有符号数和无符号数