孤燈野火
          暢想的天空
          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 孤飛燕 閱讀(246) 評論(0)  編輯  收藏 所屬分類: Tag

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


          網站導航:
           
          主站蜘蛛池模板: 济南市| 绥化市| 丹棱县| 抚顺县| 新昌县| 乌拉特后旗| 广西| 修水县| 汾阳市| 贵溪市| 金秀| 五峰| 南皮县| 东平县| 丹棱县| 东源县| 安龙县| 长宁县| 武汉市| 兰考县| 肥城市| 都兰县| 泽普县| 富源县| 夏津县| 彩票| 历史| 九寨沟县| 正安县| 定远县| 洛浦县| 苍山县| 司法| 潮州市| 玛曲县| 饶河县| 蓝田县| 双辽市| 伊通| 房山区| 叶城县|