JAVAWEB之自定义TAG开发二

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014424628/article/details/50119619

上一篇我们讲了如何使用TAG简化我们的basePath获取,现在我们再来谈谈TAG另外的一个用处,使用场景说明:很多时候,我们从后台获取的时间参数(从数据库中读取的)是这种格式的:19941230123033,然而我们需要的却是这种格式的:
1994年12月30日12时30分33秒
1994年12月30日12时30分
1994年12月30日
12时30分33秒
可谓是多种多样,按照我们常规的处理方式,我们直接在设计页面的时候就确定好页面需要显示的具体格式,然后让后台去转化为我们需要的格式不就好了吗,但是正如你所看到的,也许同一个时间日期参数在一个页面需要显示多种格式,再或者当这种格式容易发生变化的时候,上面那种处理方式显然是吃力不讨好的。下面我们借用Tag的技巧来解决这个问题:
【借助上一章的工程】
首先是:tool.tdl文件

<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>base</short-name>
    <tag>
        <name>basePath</name>
        <tag-class>com.xy.tag.BasePathTag</tag-class>
        <body-content>jsp</body-content>
    </tag>
    <tag>
        <name>timeFormat</name>
        <tag-class>com.xy.tag.TimeFormatTag</tag-class>
        <body-content>jsp</body-content>
        <attribute>
            <name>date</name>
            <required>true</required><!-- 是否必填 -->
            <rtexprvalue>true</rtexprvalue><!-- 是否能够以${}方式传值 -->
        </attribute>
        <attribute>
            <name>pattern</name><!-- 字段索引名 -->
            <required>true</required><!-- 是否必填 -->
            <rtexprvalue>false</rtexprvalue><!-- 是否能够以${}方式传值 -->
        </attribute>
    </tag>
</taglib>

说明一下:<required>true/false</required><!-- 是否必填 -->指的是是否可以传入动态值(即后台传过来的数据,例如${}方式的el表达式形式)

然后就是TimeFormatTag类了

package com.xy.tag;

import java.io.IOException;
import java.text.ParseException;
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 TimeFormatTag extends TagSupport {
    private static final long serialVersionUID = 1L;
    private String date;
    private String pattern;

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    @Override
    public int doEndTag() throws JspException {
        return 0;
    }

    @Override
    public int doStartTag() throws JspException {
        JspWriter jspw = this.pageContext.getOut();
        Date d = null;
        String fin = "";
        SimpleDateFormat from = null;
        SimpleDateFormat to = new SimpleDateFormat(pattern);
        try {
            switch (date.length()) {
            case 12:
                from = new SimpleDateFormat("yyyyMMddHHmm");
            case 14:
                from = new SimpleDateFormat("yyyyMMddHHmmss");
            }
            d = from.parse(date);
            fin = to.format(d);
            jspw.print(fin);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0;
    }
}

ok,我们在index.jsp页面测试一下(部分代码):

<body>
    <Cfg:basePath/><br/>
    19941230123033的格式化<br/>
    <Cfg:timeFormat date="19941230123033" pattern="yyyy年MM月dd日hh时mm分ss秒"></Cfg:timeFormat><br/>
    <Cfg:timeFormat date="19941230123033" pattern="yyyy年MM月dd日hh时mm分"></Cfg:timeFormat><br/>
    <Cfg:timeFormat date="19941230123033" pattern="yyyy年MM月dd日"></Cfg:timeFormat><br/>
    <Cfg:timeFormat date="19941230123033" pattern="hh时mm分ss秒"></Cfg:timeFormat><br/>
  </body>

运行截图如下:
这里写图片描述
好了,一个可变格式的转换器就完成了,使用起来还是很方便的,【可以自己尝试一下动态数据】。

猜你喜欢

转载自blog.csdn.net/u014424628/article/details/50119619