JSTl 第2 課 自定義標籤 嵌套標籤

1.jsp頁面html

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.userinfo.model.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix='fmt' uri="http://java.sun.com/jsp/jstl/fmt" %>  

<%@ taglib prefix="nest" uri="WEB-INF/parentAdnChild.tld" %> 

<html>

  <body>
  1 .<%="jstl jsp" %> <br>
    1 .<%="我是jsp的java自負33" %> <br>
    2.${1==1 }
        <%
   	 pageContext.setAttribute("el", "我是${}");  
     pageContext.setAttribute("sumi", new Integer(1));
     List<Map> list = new   ArrayList();
     Map  map1= new HashMap();
     map1.put("name","張三" );
     map1.put("age",11);
     list.add(map1);
     
     Map  map2= new HashMap();   
     map2.put("name","李四" );
     map2.put("age",22); 
     list.add(map2);
     
     Map  map3= new HashMap();   
     map3.put("name","李世民" );
     map3.put("age",33); 
     list.add(map3);
     pageContext.setAttribute("pageList", list);
    %>
    
     <br>
     <nest:parent id="1">  
        <nest:child  title="姓名" value="${name}:tagValue">${name}</nest:child>  
        <nest:child  title="年齡" value="${age }:tagValue">${age }</nest:child>  
     </nest:parent>

	
  </body>
</html>

顯示效果:java

tld文件web

<?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>parentAndChild.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>parentAndChild.ChildTag</tag-class>  
        <body-content>scriptless</body-content>  
        <attribute>  
            <name>title</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>

父類標籤:瀏覽器

package parentAndChild;


import java.io.IOException;  
import java.util.HashMap;  
import java.util.Iterator;  
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;  
import java.util.Set;  

import javax.servlet.jsp.JspException;  
import javax.servlet.jsp.JspWriter;  
import javax.servlet.jsp.tagext.IterationTag;
import javax.servlet.jsp.tagext.TagSupport;  



public class ParentTag extends TagSupport {  
    private static final long serialVersionUID = 1L;  
      

    private  int current_num=-1;
    private  int   end_num=-1;
    public int getCurrent_num() {
		return current_num;
	}
	public void setCurrent_num(int current_num) {
		this.current_num = current_num;
	}
	/** 
     *  doStartTag()方法返回一個整數值,用來決定程序的後續流程。  
     * A.Tag.SKIP_BODY:表示標籤之間的內容被忽略  
     * B.Tag.EVAL_BODY_INCLUDE:表示標籤之間的內容被正常執行  
     */  
    public int doStartTag() throws JspException { 
    	System.out.println("parent dostartTag");
    	current_num=-1;
    	List	pageList= 	(List) pageContext.getAttribute("pageList");
    	end_num=pageList.size();
    	System.out.println("pageList.size=="+end_num);
        JspWriter out = pageContext.getOut();    	
        try {
			out.print("<table  border=2 style='background-color:red;border:0px;'><tr>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			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 { 
    	System.out.println("parent EndTag");
        JspWriter out = pageContext.getOut();    	
        try {
        	out.print("</table> ");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return EVAL_PAGE;  
    } 
    @Override
    public int doAfterBody() throws JspException {
    	current_num++;  //自增一次!  0的位置已經運行了一次了!
       	System.out.println("parent doAfterBody+current_num :"+current_num);
    	JspWriter out = pageContext.getOut();    	       	 
          if(current_num<end_num){
        	  	//設置參數${}到page頁面
	      		List pageList	=(List) pageContext.getAttribute("pageList");
	      		Map<String ,String>  map=	(Map) pageList.get(getCurrent_num());
	      		for(String key: map.keySet() ){
	      			Object mapvalue=map.get(key);
	      			System.out.println("key: "+key+"--value:"+mapvalue);
	      			pageContext.setAttribute(key ,mapvalue);
	      		}//設置參數${}到page頁面
	            	try {
						out.print("</tr><tr>");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
            	
               return IterationTag.EVAL_BODY_AGAIN;
          }else{
		          	try {
							out.print("</tr>");
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}  	
                return IterationTag.SKIP_BODY;
          }
    }//end fun
	public boolean isTitle() {
		// TODO Auto-generated method stub
		if(current_num==-1){
			return true;
		}
		return false;
	}

}

子標籤:less

package parentAndChild;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;  
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.IterationTag;
import javax.servlet.jsp.tagext.TagSupport;  


public class ChildTag extends TagSupport {  
      
    private static final long serialVersionUID = 1L;  
    //定義標籤屬性key,value  
    private String title;  
    private String value;

    
    
    
    public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getValue(){  
        return value;  
    }  
    public void setValue(String value){  
        this.value = value;  
    }  
      
    public int doStartTag() throws JspException {  


    	System.out.println("child doStartTag--value:"+value);
    	
        ParentTag pTag = (ParentTag) this.getParent();  
        //調用父標籤的addValue方法,給父標籤中的map賦值  
        JspWriter out = pageContext.getOut();  
        
         
        try {
        	
        	if(pTag.isTitle()){ //輸出標題
        		out.print(  "<th>"+title+"</th>");
        	}else{ //輸出身體
        		out.print(  "<td>"); // body交給頁面添加內容
        		//設置 數據到身體
        	}	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return EVAL_BODY_INCLUDE;  
    } 
    public int doEndTag() throws JspException {  
    	
    	 ParentTag pTag = (ParentTag) this.getParent();  
        JspWriter out = pageContext.getOut();  
        try {		
        	if(pTag.isTitle()){ // 是不是標題
        		//out.print(  "<td>"+title+"</td>");
        	}else{ //輸出身體
        		out.print(  "</td>");
        	}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       return EVAL_PAGE;  
    }  
}
相關文章
相關標籤/搜索