一:
1.下载jsp标准标签库
具体参考:https://jingyan.baidu.com/article/ab0b56309427b9c15afa7dd7.html
选择lib文件夹
二:简单程序
1.程序目录
2.index.jsp
1 <%@page import="java.util.ArrayList"%> 2 <%@page import="java.util.List"%> 3 <%@page import="tag.Customer"%> 4 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 5 pageEncoding="ISO-8859-1"%> 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 10 <title>Insert title here</title> 11 </head> 12 <body> 13 <% 14 List<Customer> customers=new ArrayList<>(); 15 customers.add(new Customer(1,"AA",7)); 16 customers.add(new Customer(2,"BB",8)); 17 customers.add(new Customer(3,"CC",6)); 18 customers.add(new Customer(4,"DD",5)); 19 request.setAttribute("customers", customers); 20 %> 21 <jsp:forward page="testTag.jsp"></jsp:forward> 22 </body> 23 </html>
2.Customer.java
1 package tag; 2 3 public class Customer{ 4 private int id; 5 private String name; 6 private int age; 7 public int getId() { 8 return id; 9 } 10 public void setId(int id) { 11 this.id = id; 12 } 13 public String getName() { 14 return name; 15 } 16 public void setName(String name) { 17 this.name = name; 18 } 19 public int getAge() { 20 return age; 21 } 22 public void setAge(int age) { 23 this.age = age; 24 } 25 public Customer(int id, String name, int age) { 26 super(); 27 this.id = id; 28 this.name = name; 29 this.age = age; 30 } 31 32 }
3.testTag.jsp
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 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=ISO-8859-1"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <h1>haha</h1> 12 <c:forEach items="${requestScope.customers}" var="customers"> 13 ${customers.id} ${customers.name} ${customers.age} <br> 14 </c:forEach> 15 </body> 16 </html>
4.效果
三:自定义标签
1.定义
用户定义的标记。
tag标签被转化成了一个对称为标签处理类的对象的操作。
2.标签库API
定义在javax.servlet.jsp.tagext包中
有个父接口是JspTag,有两个子接口,分别为Tag和SimpleTag。其分别对应与传统的标签和简单标签,其中简单标签可以实现传统标签的所有功能。
简单标签有一个实现类,叫SimpleTagSupport。
3.自定义标签开发的步骤
编写完成标签功能的java类(标签处理器)
编写标签库描述文件,在tld文件中对自定义标签进行描述
在jsp页面中导入和使用自定义标签
4.SimpleTag接口方法
四:实现一个简单的hello标签(没有属性)
1.目录
2.HelloSimpleTag.java
1 package tag; 2 3 import java.io.IOException; 4 5 import javax.servlet.jsp.JspContext; 6 import javax.servlet.jsp.JspException; 7 import javax.servlet.jsp.tagext.JspFragment; 8 import javax.servlet.jsp.tagext.JspTag; 9 import javax.servlet.jsp.tagext.SimpleTag; 10 11 public class HelloSimpleTag implements SimpleTag{ 12 13 @Override 14 public void doTag() throws JspException, IOException { 15 System.out.println("doTag"); 16 } 17 18 @Override 19 public JspTag getParent() { 20 System.out.println("getParent"); 21 return null; 22 } 23 24 @Override 25 public void setJspBody(JspFragment arg0) { 26 System.out.println("setJspBody"); 27 } 28 29 @Override 30 public void setJspContext(JspContext arg0) { 31 System.out.println("setJspContext"); 32 } 33 34 @Override 35 public void setParent(JspTag arg0) { 36 System.out.println("setParent"); 37 } 38 39 }
3.myTag.tld
建议放在web-inf下。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 5 version="2.0"> 6 7 <!-- 描述tld文件 --> 8 <description>JSTL 1.1 core library</description> 9 <display-name>myTag core</display-name> 10 <tlib-version>1.0</tlib-version> 11 12 <!-- 下面两个很重要 --> 13 <short-name>jun</short-name> 14 <uri>http://java.caojun.com/jsp/myTag/core</uri> <!--相当与tld文件的ID--> 15 16 <!-- 描述自定义的标签 --> 17 <tag> 18 <!-- 标签名 --> 19 <name>hello</name> 20 <!-- 标签所在的全类名 --> 21 <tag-class>tag.HelloSimpleTag</tag-class> 22 <!-- 标签体类型 --> 23 <body-content>empty</body-content> 24 </tag> 25 </taglib>
4.test.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!-- 导入标签库 --> 4 <%@taglib uri="http://java.caojun.com/jsp/myTag/core" prefix="jun"%> 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=ISO-8859-1"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <jun:hello/> 13 </body> 14 </html>
5.效果
五:实现带属性的自定义标签(在上面的程序上进行修改)
1.顺序
现在标签处理器类中定义setter方法,这些属性是tld中要使用的属性,建议把所有的属性定义类型都设置为String类型。
在tld描述文件中描述熟属性
在页面中使用属性,属性名需同tld文件中定义的名字相同
2.HelloSimpleTag.java
1 package tag; 2 3 import java.io.IOException; 4 5 import javax.servlet.jsp.JspContext; 6 import javax.servlet.jsp.JspException; 7 import javax.servlet.jsp.JspWriter; 8 import javax.servlet.jsp.PageContext; 9 import javax.servlet.jsp.tagext.JspFragment; 10 import javax.servlet.jsp.tagext.JspTag; 11 import javax.servlet.jsp.tagext.SimpleTag; 12 13 public class HelloSimpleTag implements SimpleTag{ 14 //tld文件中将要使用的标签属性 15 private String value; 16 private String count; 17 public void setValue(String value) { 18 this.value=value; 19 } 20 public void setCount(String count) { 21 this.count=count; 22 } 23 //标签体逻辑实际编写到该方法中 24 @Override 25 public void doTag() throws JspException, IOException { 26 System.out.println("doTag"); 27 JspWriter out=pagecontext.getOut(); 28 int c=0; 29 c=Integer.parseInt(count); 30 for(int i=0;i<c;i++) { 31 out.print((i+1)+":"+value); 32 out.print("<br>"); 33 } 34 } 35 36 @Override 37 public JspTag getParent() { 38 System.out.println("getParent"); 39 return null; 40 } 41 42 @Override 43 public void setJspBody(JspFragment arg0) { 44 System.out.println("setJspBody"); 45 } 46 47 //JSP引擎调用,将jsp页面中的Pagecontext传到标签处理类中 48 //PageContext可以获取惊悚片中其他8个隐含对象 49 private PageContext pagecontext; 50 @Override 51 public void setJspContext(JspContext arg0) { 52 System.out.println("setJspContext"); 53 this.pagecontext=(PageContext) arg0; 54 } 55 56 @Override 57 public void setParent(JspTag arg0) { 58 System.out.println("setParent"); 59 } 60 61 }
3.myTag.tld
1 <?xml version="1.0" encoding="UTF-8"?> 2 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 5 version="2.0"> 6 7 <!-- 描述tld文件 --> 8 <description>JSTL 1.1 core library</description> 9 <display-name>myTag core</display-name> 10 <tlib-version>1.0</tlib-version> 11 12 <!-- 下面两个很重要 --> 13 <short-name>jun</short-name> 14 <uri>http://java.caojun.com/jsp/myTag/core</uri> 15 16 <!-- 描述自定义的标签 --> 17 <tag> 18 <!-- 标签名 --> 19 <name>hello</name> 20 <!-- 标签所在的全类名 --> 21 <tag-class>tag.HelloSimpleTag</tag-class> 22 <!-- 标签体类型 --> 23 <body-content>empty</body-content> 24 <!-- 描述当前标签的属性 --> 25 <attribute> 26 <name>value</name> 27 <required>true</required> 28 <!-- runtime expression value 是否可以接受表达式动态值 --> 29 <rtexprvalue>true</rtexprvalue> 30 </attribute> 31 <attribute> 32 <name>count</name> 33 <required>false</required> 34 <rtexprvalue>true</rtexprvalue> 35 </attribute> 36 </tag> 37 </taglib>
4.test.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!-- 导入标签库 --> 4 <%@taglib uri="http://java.caojun.com/jsp/myTag/core" prefix="jun"%> 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=ISO-8859-1"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <!-- 将value打印count次 --> 13 <jun:hello value="heha" count="9"/> 14 </body> 15 </html>
5.效果
六:实现自定义标签(可以读取工程中的文件到页面上)
1.项目目录
2.ReadFileTag.java
1 package tag; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.InputStreamReader; 7 8 import javax.servlet.jsp.JspException; 9 import javax.servlet.jsp.PageContext; 10 import javax.servlet.jsp.tagext.SimpleTagSupport; 11 12 public class ReadFileTag extends SimpleTagSupport{ 13 14 //相对于web应用根目录的文件名 15 private String src; 16 public void setSrc(String src) { 17 this.src = src; 18 } 19 @Override 20 public void doTag() throws JspException, IOException { 21 PageContext pageContext=(PageContext) getJspContext(); 22 InputStream in=pageContext.getServletContext().getResourceAsStream(src); 23 BufferedReader reader=new BufferedReader(new InputStreamReader(in)); 24 String str=null; 25 while((str=reader.readLine())!=null) { 26 pageContext.getOut().write("="+str); 27 pageContext.getOut().write("<br>"); 28 } 29 30 } 31 32 }
3.myTag.tld
1 <?xml version="1.0" encoding="UTF-8"?> 2 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 5 version="2.0"> 6 7 <!-- 描述tld文件 --> 8 <description>JSTL 1.1 core library</description> 9 <display-name>myTag core</display-name> 10 <tlib-version>1.0</tlib-version> 11 12 <!-- 下面两个很重要 --> 13 <short-name>jun</short-name> 14 <uri>http://java.caojun.com/jsp/myTag/core</uri> 15 16 <!-- 描述自定义的标签hello --> 17 <tag> 18 <!-- 标签名 --> 19 <name>hello</name> 20 <!-- 标签所在的全类名 --> 21 <tag-class>tag.HelloSimpleTag</tag-class> 22 <!-- 标签体类型 --> 23 <body-content>empty</body-content> 24 <!-- 描述当前标签的属性 --> 25 <attribute> 26 <name>value</name> 27 <required>true</required> 28 <!-- runtime expression value 是否可以接受表达式动态值 --> 29 <rtexprvalue>true</rtexprvalue> 30 </attribute> 31 <attribute> 32 <name>count</name> 33 <required>false</required> 34 <rtexprvalue>true</rtexprvalue> 35 </attribute> 36 </tag> 37 38 <!-- 描述自定义的标签readFile --> 39 <tag> 40 <name>readFile</name> 41 <tag-class>tag.ReadFileTag</tag-class> 42 <body-content>empty</body-content> 43 <attribute> 44 <name>src</name> 45 <required>true</required> 46 <rtexprvalue>true</rtexprvalue> 47 </attribute> 48 </tag> 49 </taglib>
4.test.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!-- 导入标签库 --> 4 <%@taglib uri="http://java.caojun.com/jsp/myTag/core" prefix="jun"%> 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=ISO-8859-1"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <!-- 将value打印count次 --> 13 <jun:hello value="heha" count="2"/> 14 15 <!-- 读取WEB-INF下面的文件problem.txt --> 16 <jun:readFile src="/WEB-INF/problem.txt"/> 17 </body> 18 </html>
5.效果
小瑕疵,暂时没解决。
七:带标签体的自定义标签
1.程序目录
特别注意的是,tld文件在WebContentxia
2.TestFragment.java
1 package com.caojunit; 2 3 import java.io.IOException; 4 import java.io.StringWriter; 5 6 import javax.servlet.jsp.JspException; 7 import javax.servlet.jsp.tagext.JspFragment; 8 import javax.servlet.jsp.tagext.SimpleTagSupport; 9 10 public class TestFragment extends SimpleTagSupport{ 11 @Override 12 public void doTag() throws JspException, IOException { 13 JspFragment bodyContent = getJspBody(); 14 //JspFrament.invoke(Writer);writer即为标签提输出的字符流 15 //若为null,则getContext().getout(),即输出到页面上 16 StringWriter sw=new StringWriter(); 17 bodyContent.invoke(sw); 18 String content=sw.toString().toUpperCase(); 19 getJspContext().getOut().print(content); 20 } 21 }
3.myTag.tld
1 <?xml version="1.0" encoding="UTF-8"?> 2 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 5 version="2.0"> 6 7 <description>myTag 1.3 core library</description> 8 <display-name>myTag core</display-name> 9 <tlib-version>1.3</tlib-version> 10 <short-name>jun</short-name> 11 <uri>http://caojun.com/jsp/core</uri> 12 13 <tag> 14 <name>testFragment</name> 15 <tag-class>com.caojunit.TestFragment</tag-class> 16 <body-content>scriptless</body-content> 17 </tag> 18 </taglib>
4.index.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@ taglib prefix="jun" uri="http://caojun.com/jsp/core" %> 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=ISO-8859-1"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <jun:testFragment>hello world</jun:testFragment> 12 </body> 13 </html>
5.效果
6.解释细节问题
body-content标签
scriptless:标签体可以包含el表达式和JSP动作元素,但是不能包含JSP脚本元素。
tagdependent:表示标签体交由标签本身解析处理,若是这么指定,在标签体中的所有代码都会原封不动的交给标签处理器。
八:带父标签的自定义标签
1.程序目录
2.ParentTag.java
1 package jun.com; 2 3 import java.io.IOException; 4 5 import javax.servlet.jsp.JspException; 6 import javax.servlet.jsp.tagext.SimpleTagSupport; 7 8 public class ParentTag extends SimpleTagSupport{ 9 private String name="jun"; 10 public String getName() { 11 return name; 12 } 13 @Override 14 public void doTag() throws JspException, IOException { 15 System.out.println("父标签处理类name:"+name); 16 getJspBody().invoke(null); 17 } 18 }
3.ChildTag.java
1 package jun.com; 2 3 import java.io.IOException; 4 5 import javax.servlet.jsp.JspException; 6 import javax.servlet.jsp.tagext.JspTag; 7 import javax.servlet.jsp.tagext.SimpleTagSupport; 8 9 public class ChildTag extends SimpleTagSupport{ 10 @Override 11 public void doTag() throws IOException{ 12 //得到父标签的引用 13 JspTag parent=getParent(); 14 15 ParentTag parentTag=(ParentTag) parent; 16 String name=parentTag.getName(); 17 18 getJspContext().getOut().print("子标签输出name "+name); 19 } 20 } 21
4.myTag.tld
1 <?xml version="1.0" encoding="UTF-8"?> 2 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 5 version="2.0"> 6 7 <description>JSTL 1.4 core library</description> 8 <display-name>myTag core</display-name> 9 <tlib-version>1.4</tlib-version> 10 <short-name>jn</short-name> 11 <uri>http://java.jun.com/jsp/myTag/core</uri> 12 13 <tag> 14 <name>parentTag</name> 15 <tag-class>jun.com.ParentTag</tag-class> 16 <body-content>scriptless</body-content> 17 </tag> 18 19 <tag> 20 <name>childTag</name> 21 <tag-class>jun.com.ChildTag</tag-class> 22 <body-content>empty</body-content> 23 </tag> 24 </taglib>
5.index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="jn" uri="http://java.jun.com/jsp/myTag/core"%> 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=ISO-8859-1"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <!-- 父标签将name打印到控制台 --> 12 <jn:parentTag> 13 <!-- 子标签以父标签的标签体存在,把父标签的name属性打印到JSP的页面上 --> 14 <jn:childTag/> 15 </jn:parentTag> 16 </body> 17 </html>
6.效果
7.解释
父标签无法获取子标签的引用,父标签仅把子标签作为标签体来使用。
子标签可以通过getParent()方法来获取父标签的引用。
父标签的类型是JspTag,该接口是空接口,但是用来统一SimpleTag与Tag的,实际使用需要进行类型的强制转换。
在tld文件中,无需为父标签进行额外的配置,子标签是以标签体的形式存在,所以父标签的body-content的设置为scriptless。