java 标签的制作

最近想学习Struct2 看那些标签 不tai好懂,所以我决定先学习怎么制作标签,那样学好狠多


package com.jxr;

import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.JspException;
import java.awt.*;
import java.io.IOException;

/**
 * [email protected]
 * User: Administrator
 * Date: 2009-12-3
 * Time: 21:48:26
 */
public class Timer extends TagSupport {

    private String timerId;

    public void setTimerId(String timerId) {
        this.timerId = timerId;
    }

    public void setTimer(int timer) {
        this.timer = timer;
    }

    public String getTimerId() {
        return timerId;
    }

    public int getTimer() {
        return timer;
    }

    private int timer;

    PageContext pc = null;//声明pagecontext对象

    /**
     * 设置页面的上下文 第一步
     */
    public void setPageContext(PageContext arg0) {
        pc = arg0;
        super.setPageContext(arg0);
    }

    /**
     * 设置父标签 第二步
     */
    public void setParent(Tag arg0) {
        super.setParent(arg0);
    }


    /**
     * 开始标签要执行的 第三步
     */
    public int doStartTag() throws javax.servlet.jsp.JspException {
        JspWriter writer = pc.getOut();
        try {
            writer.write(prepareJs(getTimerId(), getTimer()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return TagSupport.EVAL_PAGE;//根据返回值看看标签结束后要怎样执行别的代码
    }

    /**
     * 标签结束后的内容 第四步
     */
    public int doEndTag() throws JspException {
        return TagSupport.EVAL_PAGE;//根据返回值看看标签结束后要怎样执行别的代码
    }

    /**
     * 释放资源 第五步
     */
    public void release() {
        super.release();
    }

    private String prepareJs(String timerId, int timer) {
        StringBuffer sb = new StringBuffer();
        sb.append(String.format("<div id=\"%s\"></div>\n", timerId));
        sb.append("<script type=\"text/javascript\"  language=\"javascript\">\n");
        sb.append("var timer = function(_id, skipTime) {\n");
        sb.append("        this.id = _id;    \n");
        sb.append("        this.skipTime = skipTime; \n");
        sb.append("        };\n");
        sb.append("timer.prototype = { \n");
        sb.append("       start:function() {  \n");
        sb.append("        var _self = this;   \n");
        sb.append("        _self.timerId = window.setInterval(function() {                                 \n");
        sb.append("        var today = new Date();                     \n");
        sb.append("        var year = today.getYear() + 1900;       \n");
        sb.append("        var month = today.getMonth() < 9 ? \"0\" + ( today.getMonth() + 1) : (today.getMonth() + 1); \n");
        sb.append("       var day = today.getDate() < 10 ? \"0\" + today.getDate() : today.getDate();              \n");
        sb.append("       var hour = today.getHours() < 10 ? \"0\" + today.getHours() : today.getHours();          \n");
        sb.append("        var minute = today.getMinutes() < 10 ? \"0\" + today.getMinutes() : today.getMinutes();   \n");
        sb.append("       var secord = today.getSeconds() < 10 ? \"0\" + today.getSeconds() : today.getSeconds();  \n");
        sb.append("        document.getElementById(_self.id).innerHTML = year + \"-\" + month + \"-\" + day + \" \" + hour + \":\" + minute + \":\" + secord;\n");
        sb.append("        }, _self.skipTime); \n");
        sb.append("},\n");
        sb.append("stop:function() { \n");
        sb.append("        var tId = this.timerId;    \n");
        sb.append("        if (tId) {      ");
        sb.append("       window.clearTimeout(tId);  \n");
        sb.append("         } \n");
        sb.append("       }   \n");
        sb.append("     } \n");
        sb.append(String.format("var t_%s = new timer(\"%s\", %d);\n", timerId, timerId, timer));
        sb.append(String.format("t_%s.start();\n", timerId));
        sb.append("</script>\n");
        return sb.toString();
    }
}



你可能感兴趣的:(java,jsp,servlet,prototype)