日期:2014-05-16  浏览次数:20489 次

自定Jsp标签---作业--日期输出转换
package com.hbsi.web.tag;  
  
import java.io.IOException;  
import javax.servlet.jsp.JspException;  
import javax.servlet.jsp.JspWriter;  
import javax.servlet.jsp.tagext.BodyContent;  
import javax.servlet.jsp.tagext.BodyTagSupport;  
import javax.servlet.jsp.tagext.Tag;  
  
public class ViewTMTag extends BodyTagSupport {  
      
    @Override  
    public int doEndTag() throws JspException {  
        BodyContent bc = this.getBodyContent();  
        String b = bc.getString();  
        String[] data = b.split("-");  
        JspWriter out = this.pageContext.getOut();  
        try {  
            out.println("年:"+data[0]+"<br>");  
            out.println("月:"+data[1]+"<br>");  
            out.println("日:"+data[2]+"<br>");  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return Tag.EVAL_PAGE;  
    }  
 






<?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:schemaLocation="http://java.sun.com/xml/ns/j2ee 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>hbsi</short-name>
    <uri>http://www.hbsi.com</uri>
    <tag>
<name>viewIP</name>
<tag-class>com.hbsi.web.tag.ViewIPTag</tag-class>
<body-content>empty</body-content>
</tag>
    
      <tag>  
        <name>ViewTM</name>  
        <tag-class>com.hbsi.web.tag.ViewTMTag</tag-class>  
        <body-content>JSP</body-content>  
    </tag>  

</taglib>











<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.hbsi.com" prefix="hbsi" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DT