自定义标签
JSP自定义标签
一般我们说自定义标签是指JSP自定义标签。自定义标签在功能上逻辑上与javaBean 类似,都封装Java 代码。自定义标签是可重用的组件代码,并且允许开发人员为复杂的操作提供逻辑名称。
①使用Jsp自定义标签写一个HelloWorld.
先要写一个tld文件:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <taglib xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" 5 version="2.1"> 6 <tlib-version>1.0</tlib-version> 7 <short-name>java1234Tag</short-name> 8 <tag> 9 <name>helloWorld</name> 10 <tag-class>com.java1234.tag.HelloWorldTag</tag-class> 11 <body-content>empty</body-content> 12 </tag> 13 </taglib>
1 package com.java1234.tag; 2 import java.io.IOException; 3 import javax.servlet.jsp.JspException; 4 import javax.servlet.jsp.JspWriter; 5 import javax.servlet.jsp.tagext.TagSupport; 6 public class HelloWorldTag extends TagSupport{ 7 private static final long serialVersionUID = 1L; 8 @Override 9 public int doStartTag() throws JspException { 10 JspWriter out=this.pageContext.getOut(); 11 try { 12 out.print("Jsp自定义标签HelloWorld"); 13 14 } catch (IOException e) { 15 // TODO Auto-generated catch block 16 e.printStackTrace(); 17 } 18 return TagSupport.SKIP_BODY;//直接跳出jsp标签(最简单的标签) 19 } 20 }
页面:
在HelloWorldTag.jsp文件的标头我们引入tld文件。<%@ taglib prefix="java1234" uri="/WEB-INF/java1234.tld" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="java1234" uri="/WEB-INF/java1234.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>helloWorldTag</title>
</head>
<body>
<java1234:helloWorld/>
</body>
</html>
②自定义标签有属性的标签
传递一个参数name,需要在配置文件里面设置如下:(<required>yes</required><!-- 表示该属性,一定要使用 --> <rtexprvalue>true</rtexprvalue><!-- 属性是否能用表达式 -->)
1 <tag> 2 <name>helloWorld2</name> 3 <tag-class>com.java1234.tag.HelloWorldTag2</tag-class> 4 <body-content>empty</body-content> 5 <attribute> 6 <name>name</name>//带有name 7 <required>true</required>//为了在页面 8 <rtexprvalue>true</rtexprvalue> 9 </attribute> 10 </tag>
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 public class HelloWorldTag2 extends TagSupport{ 9 private String name;//定义name属性 10 // set & get 方法 11 public String getName() { 12 return name; 13 } 14 public void setName(String name) { 15 this.name = name; 16 } 17 18 19 private static final long serialVersionUID = 1L; 20 @Override 21 public int doStartTag() throws JspException { 22 JspWriter out=this.pageContext.getOut(); 23 try { 24 out.print(name+"Jsp自定义标签"); 25 26 } catch (IOException e) { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } 30 return TagSupport.SKIP_BODY;//直接跳出jsp标签(最简单的标签) 31 } 32 }
页面部分
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 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>helloWorldTag2</title> 9 </head> 10 <body> 11 <java1234:helloWorld2 name="带属性的jsp自定义标签屌爆了!"/> 12 </body> 13 </html>
③有标签体的自定义标签
我们在设置属性“集合”,“元素”,“遍历器”。操作如下
1 package com.java1234.tag; 2 import java.util.Iterator; 3 import java.util.List; 4 import javax.servlet.jsp.JspException; 5 import javax.servlet.jsp.tagext.TagSupport; 6 public class Iterate extends TagSupport{ 7 private String var;//集合中的每一个元素 8 private String items;//"集合" 9 private Iterator iter;//遍历器用来放置数据的。 10 11 public String getVar() { 12 return var; 13 } 14 public void setVar(String var) { 15 this.var = var; 16 } 17 public String getItems() { 18 return items; 19 } 20 public void setItems(String items) { 21 this.items = items; 22 } 23 public Iterator getIter() { 24 return iter; 25 } 26 public void setIter(Iterator iter) { 27 this.iter = iter; 28 } 29 private static final long serialVersionUID = 1L; 30 @Override 31 public int doStartTag() throws JspException { 32 //获取集合数据items----->监测数据是否存在 ----->存在并且是List --->搞成遍历器 调用方法 ----如果有值-->我进行遍历执行标签体 33 Object value=this.pageContext.getAttribute(items); 34 if(value!=null && value instanceof List ){ //判断value是不是List 35 this.iter=((List)value).iterator(); 36 if(iter.hasNext()){ 37 this.pageContext.setAttribute(var, iter.next()); 38 return TagSupport.EVAL_BODY_INCLUDE;//跳到标签体去执行操作。 39 }else{ 40 return TagSupport.SKIP_BODY;//直接跳出标签体,结束任务 41 } 42 }else{ 43 return TagSupport.SKIP_BODY;//跳出标签体,结束任务。 44 } 45 } 46 @Override 47 //如果iter还没有执行完,我就在执行一次标签体! 48 public int doAfterBody() throws JspException { 49 if(iter.hasNext()){ 50 this.pageContext.setAttribute(var, iter.next()); 51 return TagSupport.EVAL_BODY_AGAIN;//在执行一次标签体 52 }else{ 53 return TagSupport.SKIP_BODY;//直接跳出标签体 54 } 55 } 56 57 }
配置文件tld;
1 <?xml version="1.0" encoding="utf-8" ?> 2 <taglib xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" 5 version="2.1"> 6 <tlib-version>1.0</tlib-version> 7 <short-name>java1234Tag</short-name> 8 <tag> 9 <name>iterate</name> 10 <tag-class>com.java1234.tag.Iterate</tag-class> 11 <body-content>JSP</body-content> 12 <attribute> 13 <name>items</name> 14 <required>true</required> 15 <rtexprvalue>true</rtexprvalue> 16 </attribute> 17 <attribute> 18 <name>var</name> 19 <required>true</required> 20 <rtexprvalue>true</rtexprvalue> 21 </attribute> 22 </tag> 23 24 </taglib>
页面 iterate.jsp
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>iterate</title> 10 </head> 11 <body> 12 <% 13 List people =new ArrayList(); 14 people.add("张三丰2"); 15 people.add("丝丝光2"); 16 people.add("王二小2"); 17 pageContext.setAttribute("people", people); 18 %> 19 <java1234:iterate2 var="p" iteams="people"> 20 ${p } 21 </java1234:iterate2> 22 </body> 23 </html>
④简单标签
配置文件:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <taglib xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" 5 version="2.1"> 6 <tlib-version>1.0</tlib-version> 7 <short-name>java1234Tag</short-name> 8 <tag> 9 <name>iterate2</name> 10 <tag-class>com.java1234.tag.IterateSimple</tag-class> 11 <body-content>scriptless</body-content> 12 <attribute> 13 <name>var</name> 14 <required>true</required> 15 <rtexprvalue>true</rtexprvalue> 16 </attribute> 17 <attribute> 18 <name>iteams</name> 19 <required>true</required> 20 <rtexprvalue>true</rtexprvalue> 21 </attribute> 22 </tag> 23 </taglib>
处理部分:
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 10 public class IterateSimple extends SimpleTagSupport{ 11 private String var; 12 private String iteams; 13 14 public String getVar() { 15 return var; 16 } 17 public void setVar(String var) { 18 this.var = var; 19 } 20 public String getIteams() { 21 return iteams; 22 } 23 public void setIteams(String iteams) { 24 this.iteams = iteams; 25 } 26 @Override 27 public void doTag() throws JspException, IOException { 28 Object value=this.getJspContext().getAttribute("iteams"); 29 if(value !=null && value instanceof List){ 30 Iterator iter=((List)value).iterator();//构造遍历器 31 while(iter.hasNext()){ 32 this.getJspContext().setAttribute(var, iter.next()); 33 this.getJspBody().invoke(null);//响应页面! 34 } 35 } 36 } 37 }
页面:
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>iterate</title> 10 </head> 11 <body> 12 <% 13 List people =new ArrayList(); 14 people.add("张三丰"); 15 people.add("丝丝光"); 16 people.add("王二小"); 17 pageContext.setAttribute("people", people); 18 %> 19 <java1234:iterate items="people" var="p"> 20 <h1>${p }</h1> 21 </java1234:iterate> 22 </body> 23 </html>
勤加练习!加油