小峰servlet/jsp(5)jsp自定义标签
一、自定义标签helloworld:
二、自定义有属性的标签:
HelloWorldTag.java:继承TagSupport:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.java1234.tag; 2 3 import java.io.IOException; 4 5 import javax.servlet.jsp.JspException; 6 import javax.servlet.jsp.JspWriter; 7 import javax.servlet.jsp.tagext.TagSupport; 8 9 public class HelloWorldTag extends TagSupport{ 10 11 private static final long serialVersionUID = 1L; 12 13 private String name; 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 @Override 24 public int doStartTag() throws JspException { 25 JspWriter out=this.pageContext.getOut(); 26 try { 27 out.println(name + "自定义标签你好!"); 28 } catch (IOException e) { 29 e.printStackTrace(); 30 } 31 return TagSupport.SKIP_BODY; // 直接结束标签 32 } 33 }
java1234.tld描述文件:存放在/WEB-INF/java1234.tld
tld文件:专门是用来识别标签;标签库描述文件;在jsp页面引入,找到标签处理类;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <taglib xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" 6 version="2.1"> 7 <tlib-version>1.0</tlib-version> 8 <short-name>java1234Tag</short-name> 9 10 <tag> 11 <name>helloWorld</name> 12 <tag-class>com.java1234.tag.HelloWorldTag</tag-class> 13 <!-- 标签体 --> 14 <body-content>empty</body-content> 15 <attribute> 16 <name>name</name> 17 <required>true</required> <!-- required true必须要写的属性 --> 18 <rtexprvalue>true</rtexprvalue> <!-- 是否支持el表达式 true支持 --> 19 </attribute> 20 </tag> 21 22 </taglib>
helloWorldTag.jsp:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="java1234" uri="/WEB-INF/java1234.tld" %> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <java1234:helloWorld name="JspServlet屌炸天"/> 11 </body> 12 </html>
页面输出:
三、自定义有标签体的标签;
IterateTag.java:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.java1234.tag; 2 3 import java.util.Iterator; 4 import java.util.List; 5 6 import javax.servlet.jsp.JspException; 7 import javax.servlet.jsp.tagext.TagSupport; 8 9 public class IterateTag extends TagSupport{ 10 11 private static final long serialVersionUID = 1L; 12 13 private String var; 14 private String items; 15 private Iterator iter; 16 17 public String getVar() { 18 return var; 19 } 20 21 public void setVar(String var) { 22 this.var = var; 23 } 24 25 public String getItems() { 26 return items; 27 } 28 29 public void setItems(String items) { 30 this.items = items; 31 } 32 33 public Iterator getIter() { 34 return iter; 35 } 36 37 public void setIter(Iterator iter) { 38 this.iter = iter; 39 } 40 41 @Override 42 public int doStartTag() throws JspException { 43 Object value=this.pageContext.getAttribute(items); 44 if(value!=null && value instanceof List){ 45 this.iter=((List)value).iterator(); 46 if(iter.hasNext()){ 47 this.pageContext.setAttribute(var, iter.next()); 48 return TagSupport.EVAL_BODY_INCLUDE; //执行标签体 49 }else{ 50 return TagSupport.SKIP_BODY; //退出标签执行 51 } 52 }else{ 53 return TagSupport.SKIP_BODY; //退出标签执行 54 } 55 } 56 57 //标签体执行完的操作 58 @Override 59 public int doAfterBody() throws JspException { 60 if(iter.hasNext()){ 61 this.pageContext.setAttribute(var, iter.next()); 62 return TagSupport.EVAL_BODY_AGAIN; //再执行一次标签体 63 }else{ 64 return TagSupport.SKIP_BODY; //退出标签体执行 65 } 66 } 67 }
标签库描述文件java1234.tld:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <taglib xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" 6 version="2.1"> 7 <tlib-version>1.0</tlib-version> 8 <short-name>java1234Tag</short-name> 9 10 <!-- 定义items和var都是必须属性 --> 11 <tag> 12 <name>iterate</name> 13 <tag-class>com.java1234.tag.IterateTag</tag-class> 14 <body-content>JSP</body-content> 15 <attribute> 16 <name>var</name> 17 <required>true</required> 18 <rtexprvalue>true</rtexprvalue> 19 </attribute> 20 <attribute> 21 <name>items</name> 22 <required>true</required> 23 <rtexprvalue>true</rtexprvalue> 24 </attribute> 25 </tag> 26 </taglib>
iterateTag.jsp:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <%@ page import="java.util.*" %> 3 <%@ taglib prefix="java1234" uri="/WEB-INF/java1234.tld" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 <% 10 List people=new ArrayList(); 11 people.add("王二小"); 12 people.add("丝丝光"); 13 people.add("草泥马"); 14 pageContext.setAttribute("people", people); 15 %> 16 </head> 17 <body> 18 <java1234:iterate items="people" var="p"> 19 <h2>${p }</h2> 20 </java1234:iterate> 21 </body> 22 </html>
访问,页面展示:
四、简单标签:
IterateSimpleTag.java:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.java1234.tag; 2 3 import java.io.IOException; 4 import java.util.Iterator; 5 import java.util.List; 6 7 import javax.servlet.jsp.JspException; 8 import javax.servlet.jsp.tagext.SimpleTagSupport; 9 import javax.servlet.jsp.tagext.TagSupport; 10 11 public class IterateSimpleTag extends SimpleTagSupport{ 12 13 private static final long serialVersionUID = 1L; 14 15 private String var; 16 private String items; 17 18 public String getVar() { 19 return var; 20 } 21 22 public void setVar(String var) { 23 this.var = var; 24 } 25 26 public String getItems() { 27 return items; 28 } 29 30 public void setItems(String items) { 31 this.items = items; 32 } 33 34 @Override 35 public void doTag() throws JspException, IOException { 36 Object value=this.getJspContext().getAttribute(items); 37 if(value!=null && value instanceof List){ 38 Iterator iter=((List)value).iterator(); 39 while(iter.hasNext()){ 40 this.getJspContext().setAttribute(var, iter.next()); 41 this.getJspBody().invoke(null); //响应页面 42 } 43 } 44 } 45 }
java1234.tld:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <taglib xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" 6 version="2.1"> 7 <tlib-version>1.0</tlib-version> 8 <short-name>java1234Tag</short-name> 9 10 <tag> 11 <name>iterate2</name> 12 <tag-class>com.java1234.tag.IterateSimpleTag</tag-class> 13 <body-content>scriptless</body-content> 14 <attribute> 15 <name>var</name> 16 <required>true</required> 17 <rtexprvalue>true</rtexprvalue> 18 </attribute> 19 <attribute> 20 <name>items</name> 21 <required>true</required> 22 <rtexprvalue>true</rtexprvalue> 23 </attribute> 24 </tag> 25 </taglib>
iterateSimpleTag.jsp:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.util.*" %> 4 <%@ taglib prefix="java1234" uri="/WEB-INF/java1234.tld" %> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>Insert title here</title> 10 <% 11 List people=new ArrayList(); 12 people.add("王二小2"); 13 people.add("丝丝光2"); 14 people.add("草泥马2"); 15 pageContext.setAttribute("people", people); 16 %> 17 </head> 18 <body> 19 <java1234:iterate2 items="people" var="p"> 20 <h2>${p }</h2> 21 </java1234:iterate2> 22 </body> 23 </html>