程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> 自定義一個很簡單的JSP標簽

自定義一個很簡單的JSP標簽

編輯:關於JSP

Web 工程,新建Java類:

[java]  package leon.webtest.tags; 
 
import java.text.SimpleDateFormat; 
import java.util.Date; 
 
import javax.servlet.jsp.JspException; 
import javax.servlet.jsp.JspWriter; 
import javax.servlet.jsp.tagext.TagSupport; 
 
 
public class DateTime extends TagSupport { 
 
    private static final long serialVersionUID = 1L; 
 
    public int doStartTag() throws JspException { 
        try { 
            JspWriter out = this.pageContext.getOut(); 
             
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
             
            out.println("<span>"); 
            out.println(sdf.format(new Date())); 
            out.println("</span>"); 
 
        } catch (Exception e) { 
            throw new JspException(e.getMessage()); 
        } 
        return SKIP_BODY; 
    } 

package leon.webtest.tags;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;


public class DateTime extends TagSupport {

 private static final long serialVersionUID = 1L;

 public int doStartTag() throws JspException {
  try {
   JspWriter out = this.pageContext.getOut();
   
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
   
   out.println("<span>");
   out.println(sdf.format(new Date()));
   out.println("</span>");

  } catch (Exception e) {
   throw new JspException(e.getMessage());
  }
  return SKIP_BODY;
 }
}
新建tld文件:WEB-INF/tld/webtest-tags.tld

[html]  <?xml version="1.0" encoding="utf-8" ?> 
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemalLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd" 
    version="2.0"> 
 
    <description>A tag library exercising SimpleTag handlers.</description> 
    <tlib-version>1.0</tlib-version> 
    <short-name>Examples</short-name> 
    <uri>/MyTags</uri> 
    <description>JSP Self-define Tag Library.</description> 
 
    <tag> 
        <description>This is an date time tag</description> 
        <name>datetime</name> 
        <tag-class>leon.webtest.tags.DateTime</tag-class> 
        <body-content>empty</body-content> 
    </tag> 
</taglib> 

<?xml version="1.0" encoding="utf-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemalLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
 version="2.0">

 <description>A tag library exercising SimpleTag handlers.</description>
 <tlib-version>1.0</tlib-version>
 <short-name>Examples</short-name>
 <uri>/MyTags</uri>
 <description>JSP Self-define Tag Library.</description>

 <tag>
  <description>This is an date time tag</description>
  <name>datetime</name>
  <tag-class>leon.webtest.tags.DateTime</tag-class>
  <body-content>empty</body-content>
 </tag>
</taglib>
web.xml中的配置:

[html] <?xml version="1.0" encoding="UTF-8"?> 
<web-app id="WebApp_ID" version="2.4"  
    xmlns="http://java.sun.com/xml/ns/j2ee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
     
    <display-name>WebTest</display-name> 
     
    <welcome-file-list> 
        <welcome-file>index.html</welcome-file> 
        <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
     
    <jsp-config> 
        <taglib> 
            <taglib-uri>/MyTags</taglib-uri> 
            <taglib-location>/WEB-INF/tld/webtest-tag.tld 
            </taglib-location> 
        </taglib> 
    </jsp-config> 
</web-app> 

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
 <display-name>WebTest</display-name>
 
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 
 <jsp-config>
  <taglib>
   <taglib-uri>/MyTags</taglib-uri>
   <taglib-location>/WEB-INF/tld/webtest-tag.tld
   </taglib-location>
  </taglib>
 </jsp-config>
</web-app>
JSP中的使用:

首先是導入:

<%@ taglib uri="/MyTags" prefix="leon" %>

 

使用標簽:

<leon:datetime />

 

加強版:需要顯示出來的時間是動態的,像時鐘一樣會跳秒的。

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved