Jsp:taglib实现
时间:2014-05-04 19:31:11
收藏:0
阅读:441
web应用的结构:
web.xml
classes diegoyun OutputTag.class
WEB-INF src diegoyun OutputTag.java
mytag tlds diego.tld
tag.jsp
细节:
web.xml:
1 <?xml version="1.0" encoding="GBK"?> 2 <web-app 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 5 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 6 version="3.0"> 7 </web-app>
OutputTag.java:
1 package diegoyun; 2 import javax.servlet.jsp.JspException; 3 import javax.servlet.jsp.JspWriter; 4 import javax.servlet.jsp.tagext.TagSupport; 5 6 public class OutputTag extends TagSupport 7 { 8 private String name=null; 9 public void setName(String name) 10 { 11 this.name = name; 12 } 13 14 public int doStartTag() throws JspException{ 15 try 16 { 17 JspWriter out = pageContext.getOut(); 18 out.print("Hello! " + name); 19 } 20 catch (Exception e) 21 { 22 throw new JspException(e); 23 } 24 return EVAL_PAGE; 25 } 26 }
diego.tld:
1 <?xml version="1.0" encoding="GBK" ?> 2 3 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd" 6 version="2.0"> 7 <tlib-version>1.0</tlib-version> 8 <short-name>diego</short-name> 9 <!--OutputTag--> 10 <tag> 11 <name>out</name> 12 <tag-class>diegoyun.OutputTag</tag-class> 13 <body-content>empty</body-content> 14 <attribute> 15 <name>name</name> 16 <required>false</required> 17 <rtexprvalue>false</rtexprvalue> 18 </attribute> 19 </tag> 20 </taglib>
tag.jsp:
1 <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> 2 3 <%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 5 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 6 <html xmlns="http://www.w3.org/1999/xhtml"> 7 <body> 8 Test Tag: 9 <diego:out name="diegoyun"/> 10 </body> 11 </html>
评论(0)