jsp传统自定义实现年月日输出格式

本文介绍如何创建并使用自定义JSP标签,通过一个日期格式化标签的例子,展示了从配置到实现的具体步骤。

1、在web.xml文件里配

 <description>there are custom tags of class3g</description>
    <tlib-version>1.0</tlib-version>
    <short-name>Date</short-name>
    <uri>http://www.Date.com</uri>
    
     <tag>
        <description>demo</description>
        <name>dateTag</name>
        <tag-class>cn.csdn.web.tag.DateTag</tag-class>
        <body-content>scriptless</body-content>
    </tag> 

2、在jsp页面里写上

<date:dateTag>2011-10-12</date:dateTag>

3、编写一个实现tag接口的实现类

package cn.csdn.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;

public class DateTag extends BodyTagSupport {

	@Override
	public int doStartTag() throws JspException {

		return super.doStartTag();
	}

	@Override
	public int doEndTag() throws JspException {
		BodyContent bc = this.getBodyContent();
		String content = bc.getString();
		String[] str = content.split("-");
		String date[] = { "年", "月", "日" };
		JspWriter out = this.pageContext.getOut();

		for (int i = 0; i < str.length; i++) {
			try {
				out.write(date[i] + ":");
				out.write(str[i]);
				out.write("<br>");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return super.doEndTag();
	}

}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值