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

来源:互联网 发布:linux ruby 安装 编辑:程序博客网 时间:2024/05/20 18:51

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 {@Overridepublic int doStartTag() throws JspException {return super.doStartTag();}@Overridepublic 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 blocke.printStackTrace();}}return super.doEndTag();}}


原创粉丝点击