JSP自定义标签-嵌套


01、定义标签实现类


1.1、父类ParentTag.java


import java.io.IOException;  
import java.util.HashMap;  
import java.util.Iterator;  
import java.util.Map;  
import java.util.Set;  
import javax.servlet.jsp.JspException;  
import javax.servlet.jsp.JspWriter;  
import javax.servlet.jsp.tagext.TagSupport;  
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;
    }
    //该方法在子标签中调用,给父标签中的map赋值  
    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;  
    }
}

1.2、子类ChildTag.java

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class ChildTag extends TagSupport
{
    //定义标签属性key,value
    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();
        //调用父标签的addValue方法,给父标签中的map赋值
        pTag.addValue(key,value);
        return EVAL_BODY_INCLUDE;
    }
    public int doEndTag() throws JspException
    {
        return EVAL_PAGE;
    }
}


02、编写nested.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>  
    <uri>/nests/nested.tld</uri>  
    <!-- 父标签 -->  
    <tag>  
        <name>parent</name>  
        <tag-class>tag.nested.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.nested.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>


03、配置web.xml
<jsp-config>
    <taglib>
        <taglib-uri>/nest-tagtaglib-uri>
        <taglib-location>/WEB-INF/tlds/nested.tldtaglib-location>
    <taglib>
<jsp-config>


04、测试

nested.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
<%@ taglib prefix="nest" uri="/WEB-INF/tlds/nested.tld" %>  
<%@ taglib prefix="n"    uri="/nests/nested.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>  

    <n:parent id="2">  
        <n:child key="key3" value="value3"/>  
        <n:child key="key4" value="value4"/>  
    </n:parent>   
</body>  
</html>


至此完成嵌套标签的编写,部署应用,访问nested.jsp就能看到效果了



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值