一、<c:if>标签
标签处理器
private boolean test;
public void setTest(boolean test) {
this.test = test;
}
@Override
public void doTag() throws JspException, IOException {
// TODO Auto-generated method stub
JspFragment jf=this.getJspBody();
jf.invoke(null);
}
Tld文件
<tag>
<name>if</name>
<tag-class>com.hbsi.web.tag.IfTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
.jsp文件
<%@taglib uri="http://www.ping.com" prefix="c" %>
<%
session.setAttribute("user","zhangsan");
%>
<c:if test="${user!=null}">
i can!<br/>
</c:if>
二、<c:foreach><c:when/><c:otherwise/></c:foreach>标签
父标签<c:foreach></c:foreach>处理器
private boolean flag=false;
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
@Override
public void doTag() throws JspException, IOException {
JspFragment jf=this.getJspBody();
jf.invoke(null);
}
子标签<c:when/>处理器
private boolean test;
public void setTest(boolean test) {
this.test = test;
}
@Override
public void doTag() throws JspException, IOException {
//获取父标签对象
ChooseTag parent=(ChooseTag)this.getParent();
if(test && !parent.isFlag()){
//处理该标签体
this.getJspBody().invoke(null);
parent.setFlag(true);
}
}
子标签<c:otherwise/>处理器
@Override
public void doTag() throws JspException, IOException {
// TODO Auto-generated method stub
ChooseTag parent=(ChooseTag)this.getParent();
if(!parent.isFlag()){
this.getJspBody().invoke(null);
parent.setFlag(true);
}
}
Tld文件
<tag>
<name>choose</name>
<tag-class>com.hbsi.web.tag.ChooseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>when</name>
<tag-class>com.hbsi.web.tag.WhenTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>otherwise</name>
<tag-class>com.hbsi.web.tag.OtherwiseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
.jsp文件
<c:choose>
<c:when test="false">aaaaaaaaa</c:when>
<c:otherwise>bbbbbb</c:otherwise>
</c:choose>
三、<c:Foreach></c: Foreach >
包括不同类型的迭代情况
3-1、链表:List
3-2、映射:Map
3-3、对象数组:
3-4、普通数组
<c:Foreach></c: Foreach >标签处理器
Collection collection=null;
if(items instanceof Map){
Map map=(Map)items;//转换类型
collection=map.entrySet();//两列转换成单列
}else if(items instanceof Collection){
collection=(Collection)items;
}/*else if(items instanceof Object[]){
Object[] objs=(Object[])items;
collection=Arrays.asList(objs);
}else if(items instanceof int[]){
}*/
else if(items.getClass().isArray()){//获的是class对象,每一种数据类型都对应着一种class
collection=new ArrayList();
int length=Array.getLength(items);
for(int i=0;i<length;i++){
collection.add(Array.get(items, i));
}
}
//不管是那种类型,最终都要导到collection中
Iterator it=collection.iterator();
while(it.hasNext()){
Object obj=it.next();//集合中的一个元素
this.getJspContext().setAttribute(var,obj);//把集合中的元素存到某个作用域中
this.getJspBody().invoke(null);//处理标签体
}
}
Tld文件
<tag>
<name>foreach</name>
<tag-class>com.hbsi.web.tag.ForEachTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
.jsp文件
<%
List list=new ArrayList();
list.add("ddd");
list.add("rrr");
list.add("ggg");
list.add("hh");
list.add("dv");
request.setAttribute("list",list);
%>
<c:foreach items="${list}" var="str">${str}</c:foreach><%--var属性的值就是存储在作用域中的属性的名称,让${str} 可以找到吸纳供应的元素--%>
<br/>-------------------------<br/>
<%
Map map=new HashMap();
map.put("aa","dudu");
map.put("bb","hehe");
map.put("cc","wuwu");
map.put("dd","lele");
request.setAttribute("map",map);
%>
<c:foreach items="${map }" var="name">
${name}
</c:foreach>
<br/>-------------------------<br/>
<%
Integer[] num={1,3,4,5,6};//对象类型的数组
request.setAttribute("num",num);
%>
<c:foreach items="${num }" var="i">
${i}
</c:foreach>
<br/>-------------------------<br/>
<%
int[]arr={1,2,3,4};//普通数组
request.setAttribute("arr",arr);
%>
<c:foreach items="${arr }" var="index">
${index}
</c:foreach>
<br/>-------------------------<br/>
<%
double[]dd={1.0,2.0,3.5,4.4};
request.setAttribute("dd",dd);
%>
<c:foreach items="${dd }" var="index">
${index}
</c:foreach>