孤灯野火
畅想的天空
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 孤飞燕 阅读(208) 评论(0)  编辑  收藏 所属分类: Tag

只有注册用户登录后才能发表评论。


网站导航: