自定义标签 foreach

java自定义标签还是比较重要的

一般分为以下几步

1,写自定义标签类,

2,写tld文件

3,写jsp页面

下面就写几个例子

一,模仿foreach标签

1.写java类

public class ForeachTag extends SimpleTagSupport {
    
    //这个参数是用来传入要遍历的数据
    private Object items;
    //这个参数用用来传入每个遍历的引用
    private String var;

    public Object getItems() {
        return items;
    }

    public void setItems(Object items) {
        this.items = items;
    }

    public String getVar() {
        return var;
    }

    public void setVar(String var) {
        this.var = var;
    }

    @Override
    public void doTag() throws JspException, IOException {
        //定义一个迭代器
        Iterator ite = null;
        //获得数据
        Object items = this.getItems();
        //获得标签体
        JspFragment jspBody = this.getJspBody();
        //判断数据类型
        if (this.getItems() instanceof Collection) {
            //得到迭代去
            ite=((Collection)this.getItems()).iterator();
        } else if (this.getItems() instanceof Object[]) {
            ite=Arrays.asList((Object[]) this.getItems()).iterator();
        } else {
            throw new RuntimeException("不能转换");
        }
        
        // 进行迭代
        while (ite.hasNext()) {
            Object obj = ite.next();
            //把当前迭代的数据放入域对象中
            getJspContext().setAttribute(var, obj);
            //输出标签体
            jspBody.invoke(null);
        }
        super.doTag();
    }

}

2.写tld文件

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>

    <tlibversion>1.2</tlibversion>
    <jspversion>1.1</jspversion>


    <shortname>mylib</shortname>
    <uri>http://www.fz.song.myLib</uri>
    
    <tag>
        <name>foreach</name>
        <tagclass>fz.song.tag.foreach.ForeachTag</tagclass>
        <bodycontent>scriptless</bodycontent>
        <attribute>
            <name>items</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>var</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>

</taglib>

3.写jsp,

加入标签库

<%@ taglib uri="http://www.fz.song.myLib" prefix="x"%>
<%

List<Map> list=new  ArrayList<Map>();

for(int i=0;i<10;i++){
    Map m=new  HashMap();
    m.put("id", i);
    m.put("name", "name"+i);
    m.put("classes", "语文");
    m.put("cj", 100-i);
    list.add(m);
}

request.setAttribute("list", list);


String[] str={"1","2","3","4","5","6","7",};
request.setAttribute("str", str);

%>



    <table>
            <tr>
                <th>编号</th>
                <th>姓名</th>
                <th>课程</th>
                <th>成绩</th>
            </tr>
        <x:foreach items="${list}" var="obj">
            <tr>
                <td>${obj.id}</td>
                <td>${obj.name}</td>
                <td>${obj.classes}</td>
                <td>${obj.cj}</td>
            </tr>
        </x:foreach>

    </table>
    
    
    
    <x:foreach items="${str }" var="a">
            ${a }<br>
        </x:foreach>
原文地址:https://www.cnblogs.com/songfahzun/p/4918906.html