JSP 自定义标签

阅读更多
CustomerInputTag类代码
package tag;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.DynamicAttributes;

@SuppressWarnings("serial")
public class CustomerInputTag extends BodyTagSupport implements
		DynamicAttributes {
	private int level;
	
	// 与tld中配置一致
	public void setLevel(int level) {
		this.level = level;
	}

	// 记录自定义属性
	private Map attributes = new HashMap();
	
	@Override
	public int doEndTag() throws JspException {
		JspWriter writer = pageContext.getOut();
		StringBuffer sb = new StringBuffer();
		sb.append(" entry : attributes.entrySet()) {
			sb.append(entry.getKey() + "=\"" + entry.getValue() + "\" ");
		}
		sb.append("/>");
		try {
			writer.print(sb);
			writer.print(getBodyContent().getString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}

	// 支持动态属性
	public void setDynamicAttribute(String uri, String localName, Object value)
			throws JspException {
		attributes.put(localName, value.toString());
	}
}

tag.tld(放在WEB-INF目录之下)
  
  
      
    "SME Web Tags"  
    1.0  
    web  
    http://com.tag/web  
    
          
        input  
        tag.CustomerInputTag
        
        JSP  
        
        	level
        	
        	false
        	
        	true
        
          
        true  
      




tags.jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="web" uri="http://com.tag/web"%>


	
		
		JSP自定义标签
	
	
		
			dasdas
		
	




打包tag标签
首先编译好Java代码
然后创建META-INF目录,然后将tld文件放入META-INF目录

  • tags.jar (2.4 KB)
  • 下载次数: 1

你可能感兴趣的:(JSP,Tag)