java jsp自定义标签
1、web.xml配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 6 <display-name>test_Servlet</display-name> 7 <jsp-config> 8 <taglib> 9 <taglib-uri>kongxh_tag</taglib-uri> 10 <taglib-location>/WEB-INF/mytag.tld</taglib-location> 11 </taglib> 12 </jsp-config> 13 <!-- 配置欢迎界面 --> 14 <welcome-file-list> 15 <welcome-file>index.jsp</welcome-file> 16 </welcome-file-list> 17 </web-app>
2、tld的编写
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd" 4 version="2.0"> 5 <tlib-version>1.0</tlib-version> 6 <jsp-version>2.0</jsp-version> 7 <short-name>firsttags</short-name> 8 <tag> 9 <name>IterTag</name> 10 <tag-class>test_servlet_package.tag.IterTag</tag-class> 11 <body-content>jsp</body-content> 12 <attribute> 13 <name>scope</name> 14 <required>true</required> 15 <rtexprvalue>true</rtexprvalue> 16 </attribute> 17 <attribute> 18 <name>id</name> 19 <required>true</required> 20 <rtexprvalue>true</rtexprvalue> 21 </attribute> 22 <attribute> 23 <name>name</name> 24 <required>true</required> 25 <rtexprvalue>true</rtexprvalue> 26 </attribute> 27 </tag> 28 <tag> 29 <name>MyBodyTag</name> 30 <tag-class>test_servlet_package.tag.MyBodyTag</tag-class> 31 <tei-class>test_servlet_package.tag.MyTagExtraInfo</tei-class> 32 <body-content>jsp</body-content> 33 <attribute> 34 <name>scope</name> 35 <required>true</required> 36 <rtexprvalue>true</rtexprvalue> 37 </attribute> 38 <attribute> 39 <name>id</name> 40 <required>true</required> 41 <rtexprvalue>true</rtexprvalue> 42 </attribute> 43 <attribute> 44 <name>name</name> 45 <required>true</required> 46 <rtexprvalue>true</rtexprvalue> 47 </attribute> 48 </tag> 49 <tag> 50 <name>MySimpleTag</name> 51 <tag-class>test_servlet_package.tag.MySimpleTag</tag-class> 52 <body-content>empty</body-content> 53 <attribute> 54 <name>format</name> 55 <required>true</required> 56 <rtexprvalue>true</rtexprvalue> 57 </attribute> 58 </tag> 59 <tag> 60 <name>MySimpleTagForIter</name> 61 <tag-class>test_servlet_package.tag.MySimpleTagForIter</tag-class> 62 <body-content>scriptless</body-content> 63 <attribute> 64 <name>scope</name> 65 <required>true</required> 66 <rtexprvalue>true</rtexprvalue> 67 </attribute> 68 <attribute> 69 <name>id</name> 70 <required>true</required> 71 <rtexprvalue>true</rtexprvalue> 72 </attribute> 73 <attribute> 74 <name>name</name> 75 <required>true</required> 76 <rtexprvalue>true</rtexprvalue> 77 </attribute> 78 </tag> 79 <tag> 80 <name>DynamicAttributesTag</name> 81 <tag-class>test_servlet_package.tag.DynamicAttributesTag</tag-class> 82 <body-content>empty</body-content> 83 <dynamic-attributes>true</dynamic-attributes> 84 </tag> 85 </taglib>
3、Tag的编写
IterTag:循环标签
1 package test_servlet_package.tag; 2 import java.util.Iterator; 3 import java.util.List; 4 import javax.servlet.jsp.JspException; 5 import javax.servlet.jsp.PageContext; 6 import javax.servlet.jsp.tagext.TagSupport; 7 public class IterTag extends TagSupport { 8 /** 9 * jsp 1.2 之前 的 做法 10 */ 11 private static final long serialVersionUID = 1L; 12 //接受属性名称 13 private String name; 14 //接受查找范围 15 private String scope; 16 private String id; 17 private Iterator<?> iter = null; 18 @Override 19 public int doStartTag() throws JspException { 20 Object value = null; 21 //是否是page范围 22 if ("page".equals(this.scope)) { 23 value = super.pageContext 24 .getAttribute(name, PageContext.PAGE_SCOPE); 25 } else if ("request".equals(this.scope)) { 26 //是否是request范围 27 value = super.pageContext.getAttribute(name, 28 PageContext.REQUEST_SCOPE); 29 } else if ("session".equals(this.scope)) { 30 //是否是session范围 31 value = super.pageContext.getAttribute(name, 32 PageContext.SESSION_SCOPE); 33 } else { 34 //是否是application范围 35 value = super.pageContext.getAttribute(name, 36 PageContext.APPLICATION_SCOPE); 37 } 38 //如果没有查到属性 39 if (value !=null && value instanceof List<?>) { 40 this.iter = ((List<?>)value).iterator(); 41 if(iter.hasNext()){ 42 super.pageContext.setAttribute(this.id,iter.next()); 43 //找到属性,执行标签题内容 44 return TagSupport.EVAL_BODY_INCLUDE; 45 }else{ 46 //找到属性,执行标签题内容 47 return TagSupport.SKIP_BODY; 48 } 49 }else{ 50 //不执行标签体内容 51 return TagSupport.SKIP_BODY; 52 } 53 } 54 public String getName() { 55 return name; 56 } 57 58 @Override 59 public int doAfterBody() throws JspException { 60 if(iter.hasNext()){ 61 super.pageContext.setAttribute(this.id,iter.next()); 62 return TagSupport.EVAL_BODY_AGAIN; //反复执行 63 }else{ 64 return TagSupport.SKIP_BODY; 65 } 66 } 67 @Override 68 public int doEndTag() throws JspException { 69 return super.doEndTag(); 70 } 71 public void setName(String name) { 72 this.name = name; 73 } 74 public String getScope() { 75 return scope; 76 } 77 public void setScope(String scope) { 78 this.scope = scope; 79 } 80 public String getId() { 81 return id; 82 } 83 public void setId(String id) { 84 this.id = id; 85 } 86 }