如何在JavaWeb程序中使用tld文件

 tld定义格式

[html] view plain copy
  1. <?xml version="1.0" encoding="ISO-8859-1" ?>  
  2. <!DOCTYPE taglib  
  3. PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"  
  4. "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">  
  5.   
  6. <taglib>  
  7. <!--定义标签版本库-->  
  8. <tlib-version>1.0</tlib-version>  
  9. <!--定义jsp版本库-->  
  10. <jsp-version>1.2</jsp-version>  
  11. <short-name>Cms Tag</short-name>  
  12. <description><!--标签描述--->  
  13. A simple appbase tag library  
  14. </description>  
  15. <tag>  
  16.   
  17. <name>page</name><!--tag的名字-->  
  18. <tag-class>com.cms.common.tag.PageTag</tag-class><!--tag对应的java类的名字-->  
  19. <body-content>empty</body-content>  
  20.   
  21. <!--关于body-content 有三个值可选;empty:标签体必须为空;jsp:标签由其他jsp元素组成比如标签中含有<%=attributeName%>的jsp元素,那么此时body-content的值就是实际attributeName传入的值;tagdependent:有标签解释不带jsp转换(这个深入的含义不太了解)-->  
  22.   
  23. <attribute><!---这里表示的是这个tag的一个参数-->  
  24. <name>cmsform</name><!--这个参数的名字-->  
  25. <required>true</required><!--是否是必填选项-->  
  26. <rtexprvalue>true</rtexprvalue><!--这个参数的值是否可以写入,换句话说 就是这个参数是否可以动态赋值-->  
  27. </attribute>  
  28.   
  29. </tag>  
  30.   
  31. </taglib>  


定义Tag对应类

此类必须重写doStartTag以及doEndTag方法

[java] view plain copy
  1. /** 
  2.  *  
  3.  */  
  4. package com.cms.common.tag;  
  5.   
  6. import javax.servlet.jsp.JspException;  
  7. import javax.servlet.jsp.JspWriter;  
  8. import javax.servlet.jsp.tagext.TagSupport;  
  9.   
  10. /** 
  11.  * @author louisliao 
  12.  * 
  13.  */  
  14. public class DemoViewTag extends TagSupport {  
  15.     /** 
  16.      *  
  17.      */  
  18.     private static final long serialVersionUID = 1L;  
  19.     private String cmsform = "";      
  20.     public String getCmsForm() {  
  21.         return cmsform ;  
  22.     }  
  23.     public void setCmsForm(String cmsform ) {  
  24.         this.cmsform = cmsform ;  
  25.     }  
  26.         /** 
  27.      *  
  28.      */  
  29.     public DemoViewTag() {  
  30.         // TODO Auto-generated constructor stub  
  31.     }  
  32.     public int doStartTag()  
  33.     {  
  34.         return super.SKIP_BODY;  
  35.     }  
  36.     public int doEndTag() throws JspException  
  37.     {  
  38.         JspWriter writer=pageContext.getOut();  
  39.         try {  
  40.             writer.print("这是我的标签示例<br/>"+"cmsform :"+this.cmsform);  
  41.         } catch (Exception e) {  
  42.             // TODO: handle exception  
  43.             e.printStackTrace();  
  44.         }  
  45.         return super.EVAL_PAGE;  
  46.     }  
  47. }  


 

在web.xml中加入taglib对应文件配置

如:

[html] view plain copy
  1. <taglib>  
  2. <taglib-uri>http://mytag.sf.net</taglib-uri>  
  3. <taglib-location>/WEB-INF/mytag.tld</taglib-location>  
  4. </taglib>  


这样就表示了http://mytag.sf.net对应WEB-INF/mytag.tld文件

在Jsp页面中引用

如:

<%@ taglib uri="http://mytag.sf.net" prefix="myTag"%>

在Jsp页面中使用

<myTag:exname1><myTag:exname1>

示例:

定义myTag.tld标签文件

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"  
  3.       "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">  
  4. <taglib>  
  5.     <tlibversion>1.0</tlibversion>  
  6.     <jspversion>1.1</jspversion>  
  7.     <shortname>MyJSPTag Library</shortname>  
  8.     <uri>http://mytag.sf.net</uri>  
  9.     <info>我的示例标签库</info>  
  10.       
  11.     <tag>  
  12.         <name>demo.Viewport</name>  
  13.         <tagclass>com.myapp.web.tag.DemoViewTag</tagclass>  
  14.         <bodycontent>JSP</bodycontent>  
  15.         <info>demo.Viewport标签</info>  
  16.         <attribute>  
  17.             <name>northTitle</name>  
  18.             <required>true</required>  
  19.             <rtexprvalue>true</rtexprvalue>  
  20.         </attribute>  
  21.         <attribute>  
  22.             <name>westTitle</name>  
  23.             <required>true</required>  
  24.             <rtexprvalue>true</rtexprvalue>  
  25.         </attribute>    
  26.     </tag>  
  27. </taglib>  


定义标签类

[java] view plain copy
  1. /** 
  2.  *  
  3.  */  
  4. package com.myapp.web.tag;  
  5.   
  6. import javax.servlet.jsp.JspException;  
  7. import javax.servlet.jsp.JspWriter;  
  8. import javax.servlet.jsp.tagext.TagSupport;  
  9.   
  10. /** 
  11.  * @author louisliao 
  12.  * 
  13.  */  
  14. public class DemoViewTag extends TagSupport {  
  15.     /** 
  16.      *  
  17.      */  
  18.     private static final long serialVersionUID = 1L;  
  19.     private String northTitle = "";  
  20.     private String westTitle = "";  
  21.       
  22.     public String getNorthTitle() {  
  23.         return northTitle;  
  24.     }  
  25.     public void setNorthTitle(String northTitle) {  
  26.         this.northTitle = northTitle;  
  27.     }  
  28.     public String getWestTitle() {  
  29.         return westTitle;  
  30.     }  
  31.     public void setWestTitle(String westTitle) {  
  32.         this.westTitle = westTitle;  
  33.     }  
  34.     /** 
  35.      *  
  36.      */  
  37.     public DemoViewTag() {  
  38.         // TODO Auto-generated constructor stub  
  39.     }  
  40.     public int doStartTag()  
  41.     {  
  42.         return super.SKIP_BODY;  
  43.     }  
  44.     public int doEndTag() throws JspException  
  45.     {  
  46.         JspWriter writer=pageContext.getOut();  
  47.         try {  
  48.             writer.print("这是我的标签示例<br/>westTitle:"+this.westTitle+"<br/>northTitle:"+this.northTitle);  
  49.         } catch (Exception e) {  
  50.             // TODO: handle exception  
  51.             e.printStackTrace();  
  52.         }  
  53.         return super.EVAL_PAGE;  
  54.     }  
  55. }  


web.xml添加配置

[html] view plain copy
  1. <taglib>  
  2. <taglib-uri>http://mytag.sf.net</taglib-uri>  
  3. <taglib-location>/WEB-INF/mytag.tld</taglib-location>  
  4. </taglib>  


测试页面

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%@ taglib uri="http://mytag.sf.net" prefix="myTag"%>   
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>My JSP 'tagtldDemo.jsp' starting page</title>  
  14.       
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.     <!-- 
  21.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  22.     -->  
  23.   
  24.   </head>  
  25.     
  26.   <body>  
  27.     This is my JSP page. <br>  
  28.     <myTag:demo.Viewport northTitle="南" westTitle="西"></myTag:demo.Viewport>  
  29.   </body>  
  30. </html>  

猜你喜欢

转载自blog.csdn.net/weixin_39816740/article/details/80643332
今日推荐