孤燈野火
          暢想的天空
          posts - 2,comments - 4,trackbacks - 0


          如定義個時間自定義標簽:

           

          <?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 web-jsptaglibrary_2_0.xsd"
              version
          ="2.0">
              
          <tlib-version>1.0</tlib-version>
              
          <short-name>uni-logi-tag</short-name>
              
          <uri>/unitag</uri>
              
             
          <tag>
                  
          <name>showdate</name>
                  
          <tag-class>com.unilogi.core.taglib.DateTag</tag-class>
                  
          <body-content>empty</body-content>
                    
                  
          <attribute>
                      
          <name>val</name>
                      
          <required>true</required>
                      
          <rtexprvalue>true</rtexprvalue>
                  
          </attribute>
                  
                   
          <attribute>
                      
          <name>timeZone</name>
                      
          <required>false</required>
                      
          <rtexprvalue>true</rtexprvalue>
                  
          </attribute>
              
          </tag>
          </taglib>

           

          3.      定義tagclass

          a)    BaseTag


           

          /**
           * 
           
          */

          package com.core.taglib;

          import java.io.IOException;

          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.jsp.JspException;
          import javax.servlet.jsp.PageContext;
          import javax.servlet.jsp.tagext.SimpleTagSupport;

          import org.slf4j.Logger;
          import org.slf4j.LoggerFactory;
          import org.springframework.beans.BeansException;
          import org.springframework.web.context.WebApplicationContext;
          import org.springframework.web.context.support.WebApplicationContextUtils;

          /**
           * 
          @author liu_dawei
           * 
           
          */

          public abstract class BaseTag extends SimpleTagSupport {

              
          private static final Logger logger = LoggerFactory.getLogger(BaseTag.class);

              
          /**
               * make the menu tree by access resources
               
          */

              
          protected abstract void doTag(HttpServletRequest request) throws JspException, IOException;

              
          /**
               * make the menu tree by access resources
               
          */

              
          public void doTag() throws JspException, IOException {
                  logger.debug(
          "doTag start");

                  
          this.doTag(this.getRequest());

                  logger.debug(
          "doTag end");
              }


              
          /**
               * get bean form application context
               * 
               * 
          @param name baen name
               * 
          @return
               * 
          @throws BeansException
               
          */

              
          protected <T> T getContextBean(String name, Class<T> requiredType) throws BeansException {

                  T bean 
          = null;

                  PageContext pageContext 
          = (PageContext) this.getJspContext();
                  WebApplicationContext wac 
          = WebApplicationContextUtils
                          .getWebApplicationContext(pageContext.getServletContext());
                  bean 
          = (T) wac.getBean(name, requiredType);

                  
          return bean;
              }


              
          /**
               * get request form application context
               * 
               * 
          @return
               
          */

              
          protected HttpServletRequest getRequest() {

                  PageContext pageContext 
          = (PageContext) this.getJspContext();
                  
          return (HttpServletRequest) pageContext.getRequest();
              }

          }

           

          b)        定義DateTage類

          /**
           * DateTag.java
           * 
           * @screen
           * 
          @author liu_dawei
           
          */

          package com.core.taglib;

          import java.io.IOException;
          import java.util.Date;

          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.jsp.JspException;
          import javax.servlet.jsp.JspWriter;

          import org.slf4j.Logger;
          import org.slf4j.LoggerFactory;

          import com.unilogi.core.util.DateTimeUtil;

          /**
           * <p>
           * DateTag.
           * </p>
           * 
           * 
          @author liu_dawei
           
          */

          public class DateTag extends BaseTag {

              
          private Date val;

              
          private String timeZone;

              
          /** logger */
              
          private static Logger logger = LoggerFactory.getLogger(DateTag.class);

              
          public Date getVal() {
                  
          return this.val;
              }


              
          public void setVal(Date val) {
                  
          this.val = val;
              }


              
          public String getTimeZone() {
                  
          return this.timeZone;
              }


              
          public void setTimeZone(String timeZone) {
                  
          this.timeZone = timeZone;
              }


              
          /**
               * <p>
               * </p>
               * 
               * 
          @param request
               * 
          @throws JspException
               * 
          @throws IOException
               * 
          @see com.unilogi.core.taglib.BaseTag#doTag(javax.servlet.http.HttpServletRequest)
               
          */


              @Override
              
          protected void doTag(HttpServletRequest request) {
                  
          // TODO Auto-generated method stub

                  
          try {

                      JspWriter out 
          = this.getJspContext().getOut();

                      out.write(DateTimeUtil.displayJspDate(val, timeZone));
                  }
           catch (Exception e) {
                      logger.error(e.getMessage());
                  }

              }

          }

          DateTimeUtil.displayJspDate(val, timeZone) 為定義格式化的時間

           4 jsp中引入自定義的tag

           例如:<%@taglib uri="/unitag" prefix="u"%>

          Jsp中需要的地方寫入

          <u:showdate  val="<%= new Date() %>" timeZone="GMT+8"/>  timeZone可空



          posted on 2012-11-01 15:42 孤飛燕 閱讀(247) 評論(0)  編輯  收藏 所屬分類: Tag

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 鸡泽县| 太谷县| 凤翔县| 福海县| 招远市| 南溪县| 宜宾县| 彰化县| 东阿县| 凯里市| 平湖市| 道孚县| 鄂尔多斯市| 堆龙德庆县| 泰和县| 安龙县| 内乡县| 丰都县| 咸丰县| 都江堰市| 石屏县| 洪泽县| 松溪县| 山西省| 乌鲁木齐市| 塔城市| 秭归县| 高邮市| 铜川市| 江西省| 遵义县| 镇江市| 湘潭县| 和龙市| 安宁市| 高雄市| 西贡区| 古田县| 嵊泗县| 临沂市| 岳阳县|