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; } }