`
312350968
  • 浏览: 209243 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

自定义标签例子

阅读更多

WEB-INF下新建xxx.tld文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" 
       "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">

<taglib>
	 <tlibversion>1.0</tlibversion>
	 <jspversion>1.0</jspversion>
	 <shortname>tiles</shortname>
	<uri>http://jakarta.apache.org/struts/tags-tiles</uri>
	
	<!-- 有属性无标签体 -->
  <tag>
     <!-- 标签名称-->
     <name>date</name>
     <!-- 标签对应的处理类-->
     <tagclass>com.clouds.util.tag.DateTag</tagclass>
     <!-- 标签体内容,没有标签体则设为empty-->
     <bodycontent>jsp</bodycontent>
     <!-- 标签的属性声明-->
     <attribute>  
            <name>value</name>  
            <required>true</required>  
            <rtexprvalue>true</rtexprvalue>  
     </attribute> 
     <attribute>  
            <name>type</name>  
            <required>false</required>  
            <rtexprvalue>true</rtexprvalue>  
     </attribute> 
     
     </tag>
  <tag>
     <!-- 标签名称-->
     <name>delivery</name>
     <!-- 标签对应的处理类-->
     <tagclass>com.clouds.util.tag.DeliveryTag</tagclass>
     <!-- 标签体内容,没有标签体则设为empty-->
     <bodycontent>jsp</bodycontent>
     <!-- 标签的属性声明-->
     <attribute>  
            <name>value</name>  
            <required>true</required>  
            <rtexprvalue>true</rtexprvalue>  
     </attribute> 
   </tag>
  <tag>
     <!-- 标签名称-->
     <name>product</name>
     <!-- 标签对应的处理类-->
     <tagclass>com.clouds.util.tag.ProductsTag</tagclass>
     <!-- 标签体内容,没有标签体则设为empty-->
     <bodycontent>jsp</bodycontent>
     <!-- 标签的属性声明-->
     <attribute>  
            <name>value</name>  
            <required>true</required>  
            <rtexprvalue>true</rtexprvalue>  
     </attribute> 
   </tag>

  
</taglib>

 新建对应的类:

/**  

* @Title: DateTag.java
* @Package com.clouds.util
* @Description: TODO(用一句话描述该文件做什么)
* @author 周张豹
* @date 2012-9-11 下午05:20:46
* @version V1.0  
*/
package com.clouds.util.tag;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * @ClassName: DateTag
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 周张豹
 * @date 2012-9-11 下午05:20:46
 *
 */

public class DateTag extends TagSupport {

    private static final long serialVersionUID = 6464168398214506236L;
    
    private String value;
    private String type;
    
    @Override
    public int doStartTag() throws JspException {
        String vv = ""+value;
        if (value != null && !"".equals(value)) {
        	Long a = Long.valueOf(value);
    		Long aa = (Long) a * 1000;
    		Date b = new Date(aa);
    		if (type == null || "".equals(type)) {
    			type = "yyyy-MM-dd HH:MM:ss";
    		}
            SimpleDateFormat sdf= new SimpleDateFormat(type);
            String date = sdf.format(b);
            try {
                pageContext.getOut().write(date+"");
            } catch (IOException e) {
                e.printStackTrace();
            }
		}
        return super.doStartTag();
    }
    public static void main(String[] args) throws JspException {
    	DateTag tag = new DateTag();
		tag.doStartTag();
	}
    public void setValue(String value) {
        this.value = value;
    }
    
    /**
	 * @return 获取 type 的值
	 */
	public String getType() {
		return type;
	}


	/**
	 * @param 设置  type 的值
	 */
	public void setType(String type) {
		this.type = type;
	}
}
页面上调用如下:
<common:data value="" type=""></common:data>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics