jsp自定义标签

阅读更多

1.定义处理类

 

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.etool.modules.curd.service.CurdService;

public class OutputTag extends TagSupport{

        //接收2个参数
	private String target;
	private String fieldName;
	
	@Override
	public int doStartTag() throws JspException {
		
		String result="";
		
		
		JspWriter out = this.pageContext.getOut();
		ServletContext servletContext = pageContext.getServletContext();
		
		//逻辑
		
		try
		{	
				out.print(result);
		}catch(IOException e)
		{
			e.printStackTrace();
		}
		
		return super.doStartTag();
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}
}

 

 

2.编辑tld文件,在WEB-INF\my-tag.tld

 




	/
	My Tag Library
	1.0
	m
	/my-tags

	
		out
		com.etool.commons.tag.OutputTag
		JSP
		
			target
			true
			true
			String
		
		
			fieldName
			true
			true
			String
		
	
	

 

 

3.配置web.xml

 

	
		
			/my-tags
			/WEB-INF/my-tags.tld
		
	

 

4.使用前

<%@ taglib uri="/my-tags" prefix="m" %>

 

这个uri和web.xml中的的值一样

 

5.使用

 

 

在eclipse会有提示

 

 

 

你可能感兴趣的:(jsp,自定义标签)