服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

使用jsp的自定义标签开发

标签的tld

<?xml version="1.0" encoding="iso-8859-1" ?>
<!doctype taglib
public "-//sun microsystems, inc.//dtd jsp tag library 1.2//en"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.2</jsp-version>
  <short-name>book</short-name>
  <uri>http://jstlbook.com/tld/weekday.tld</uri>
<tag>
  <name>if</name>
  <tag-class>ttt.conditiontest</tag-class>
  <attribute>
    <name>items</name>
   </attribute>
   <attribute>
     <name>var</name>
   </attribute>
   <attribute>
     <name>varstatus</name>
    </attribute>
</tag>
</taglib>

标签类:

几个地方需要说明

items属性是jsp页面传来的需要迭代的collection

hasnext方法返回collection是否迭代结束

next 进行迭代

divpare 可以看成是初始化

setvarstatus方法可以监视循环状态,根c:foreach的类似

package ttt;
import javax.servlet.jsp.jsptagexception;
import javax.servlet.jsp.jstl.core.looptagsupport;

public class looptagtest extends looptagsupport ...{
    private string items;
    private int i=0;
    public string getitems() ...{
        return items;
    }

    public void setitems(string items) ...{
        this.items = items;
    }

    protected boolean hasnext() throws jsptagexception ...{
        if(i==0)
        ...{
            return true;
        }else...{
            return false;
        }
    }

    protected object next() throws jsptagexception ...{
        i=1;
        return this.getitems();
    }

    protected void divpare() throws jsptagexception ...{
        // todo auto-generated method stub
        
    }

    public void setvarstatus(string arg0) ...{
        super.setvarstatus(arg0);
    }

}

jsp页面

<%...@ taglib divfix="cc" uri="http://jstlbook.com/tld/weekday.tld" %>
<%...@ taglib divfix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>

<title>currency formatting</title>
</head>
<body>
<cc:if items="a:b:c:d" var="line" varstatus="s">
  <c:out value="${s.first}"/><br>
  <c:fortokens items="${line}" delims=":" var="field">
      <c:out value="${field}"/><br>
  </c:fortokens>
</cc:if>
</body>
</html>

结果很容易,因为只有一个string,所以外层循环只循环一次,内层根据delims循环四次

true

a

b

c

d

(t007)


扫描关注微信公众号