请问如下自定义标签该如何定义:<d:test id="demo">
    <d:param name="key1" value="value" />
    <d:param name="key2" value="value2"/>
    ....
</d:test>如何在自定义标签中定义嵌套的参数.  并且可以多次使用.    在tag.class中又如何获取??知道的帮解答下,不胜感激~~

解决方案 »

  1.   

    d:test是个标签
    d:param也是个标签, 是d:test的子标签
    应该是这样的
      

  2.   

    以前写过简单的tag,嵌套的刚写了个,可以用,楼主可以参考下
    ParentTag.javaimport java.io.*;
    import java.util.*;
    import javax.servlet.jsp.*;
    public class ParentTag extends TagSupport {
    private Map<String,String> map = new HashMap<String,String>();
    private String id ;
    public String getId(){
    return id;
    }
    public void setId(String id){
    this.id = id;
    }
    public void addValue(String key,String value){
    map.put(key,value);
    }
    /**
     *  doStartTag()方法返回一个整数值,用来决定程序的后续流程。 
     * A.Tag.SKIP_BODY:表示标签之间的内容被忽略 
     * B.Tag.EVAL_BODY_INCLUDE:表示标签之间的内容被正常执行 
     */
    public int doStartTag() throws JspException {
    JspWriter out = pageContext.getOut();
    try {
    Set<String> keySet = map.keySet();
    Iterator<String> it = keySet.iterator();
    while(it.hasNext()){
    String key = it.next();
    String value = map.get(key);
    out.write(key +": <input type='text' name="+key+" value="+value+" /><br>");
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    return EVAL_BODY_INCLUDE;
    }
    /** 
     *  doEndTag:当JSP容器遇到自定义标签的结束标志,就会调用doEndTag()方法。doEndTag()方法也返回一个整数值,用来决定程序后续流程。 
        * A.Tag.SKIP_PAGE:表示立刻停止执行网页,网页上未处理的静态内容和JSP程序均被忽略任何已有的输出内容立刻返回到客户的浏览器上。 
        * B.Tag.EVAL_PAGE:表示按照正常的流程继续执行JSP网页 
     */
    public int doEndTag() throws JspException {
    return EVAL_PAGE;
    }
    }ChildTag.javaimport javax.servlet.jsp.JspException;
    import javax.servlet.jsp.tagext.TagSupport;
    public class ChildTag extends TagSupport {
    private String key;
    private String value;
    public String getKey(){
    return key;
    }
    public void setKey(String key){
    this.key = key;
    }
    public String getValue(){
    return value;
    }
    public void setValue(String value){
    this.value = value;
    }
    public int doStartTag() throws JspException {
    ParentTag pTag = (ParentTag) this.getParent();
    pTag.addValue(key,value);
    return EVAL_BODY_INCLUDE;
    }
    public int doEndTag() throws JspException {
    return EVAL_PAGE;
    }
    }
    tld文件 nest.tld<?xml version="1.0" encoding="UTF-8" ?>
    <!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>2.0</jsp-version>
    <short-name>nest</short-name>
    <!-- 父标签 -->
    <tag>
    <name>parent</name>
    <tag-class>tag.nest.ParentTag</tag-class>
    <!-- scriptless 主体可以有内容, 而jsp容器会去处理里面的jsp元素, 换句话就是可以是文本, EL表达式, 
    标准动作甚至另一个自定义标记. -->
    <body-content>scriptless</body-content>
    <attribute>
    <name>id</name>
    <required>true</required>
    <!-- rtexprvalue 是否允许表达式 不允许时${1+1}出错 -->
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    </tag>
    <!-- 子标签 -->
    <tag>
    <name>child</name>
    <tag-class>tag.nest.ChildTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
    <name>key</name>
    <required>true</required>
    <!-- rtexprvalue 是否允许表达式 不允许时${1+1}出错 -->
    <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
    <name>value</name>
    <required>true</required>
    <!-- rtexprvalue 是否允许表达式 不允许时${1+1}出错 -->
    <rtexprvalue>true</rtexprvalue>
    </attribute>
    </tag>
    </taglib>
    web.xml<jsp-config>
    <taglib>
    <taglib-uri>/nest-tag</taglib-uri>
    <taglib-location>/WEB-INF/tlds/nest.tld</taglib-location>
    </taglib>
    </jsp-config>
    nest.jsp<%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib prefix="nest" uri="/WEB-INF/tlds/nest.tld" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <nest:parent id="1">
    <nest:child key="key1" value="value1"/>
    <nest:child key="key2" value="value2"/>
    </nest:parent>
    </body>
    </html>
      

  3.   

    最好是找资料看看:http://www.blogjava.net/hackiller/archive/2009/04/14/265462.html
      

  4.   

    http://www.blogjava.net/hackiller/archive/2009/04/14/265462.html顶顶