自定义标签探究(二)

:接上篇博文

自定义标签(foreach、select)

  • foreach
      • tld文件代码
      • 助手类代码
  • select
      • tld文件代码
      • 助手类代码
  • 总结

foreach

tld文件代码

<tag>
	    <name>forEach</name>
	    <tag-class>com.rong.tag.ForEachTag</tag-class>
	    <body-content>JSP</body-content>
	    <attribute>
	        <name>items</name>
	        <required>true</required>
	        <rtexprvalue>true</rtexprvalue>
	    </attribute>
	    <attribute>
	        <name>var</name>
	        <required>true</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
	     <attribute>
	        <name>varStatus</name>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
  	</tag>

助手类代码

package com.rong.tag;

import java.util.Iterator;
import java.util.List;

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

public class ForEachTag extends BodyTagSupport{

	private List<Object> items;
	private String var;//保存集合的key
	private String varStatus;
	
	public static class Status{
		public int index=0;

		public int getIndex() {
			return index;
		}
		public void setIndex(int index) {
			this.index = index;
		}
		
		public int getCount() {
			return this.index+1;
		}
		void increment() {
			this.index++;
		}
	}
	
	public String getVarStatus() {
		return varStatus;
	}
	public void setVarStatus(String varStatus) {
		this.varStatus = varStatus;
	}

	public ForEachTag() {}

	public List<Object> getItems() {
		return items;
	}
	public void setItems(List<Object> items) {
		this.items = items;
	}

	public String getVar() {
		return var;
	}
	public void setVar(String var) {
		this.var = var;
	}
	
	@Override
	public int doStartTag() throws JspException {
		if(null!=varStatus&&!"".equals(varStatus)) {
			pageContext.setAttribute(varStatus, new Status());
			
		}
		if(null!=items&&items.size()>0) {
			Iterator<Object> it=items.iterator();
			Object next=it.next();
			pageContext.setAttribute(var, next);
			pageContext.setAttribute("it", it);
			return EVAL_BODY_INCLUDE;
		}
		return super.doStartTag();
	}
	
	@Override
	public int doAfterBody() throws JspException {
		Iterator<Object> it=(Iterator<Object>)pageContext.getAttribute("it");
		while(it.hasNext()) {
			if(null!=varStatus&&!"".equals(varStatus)) {
				Status status=(Status)pageContext.getAttribute(varStatus);
				status.increment();
				pageContext.setAttribute(varStatus, status);
			}
			Object next=it.next();
			pageContext.setAttribute(var, next);
			return EVAL_BODY_AGAIN;
		}
		return super.doAfterBody();
	}
	
}

select

tld文件代码

<tag>
	    <name>select</name>
	    <tag-class>com.rong.tag.SelectTag</tag-class>
	    <body-content>JSP</body-content>
	    <attribute>
	        <name>items</name>
	        <required>true</required>
	        <rtexprvalue>true</rtexprvalue>
	    </attribute>
	    <attribute>
	        <name>name</name>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
	    <attribute>
	        <name>valueKey</name>
	        <required>true</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
	    <attribute>
	        <name>textKey</name>
	        <required>true</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
	    <attribute>
	        <name>cssStyle</name>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
	    <attribute>
	        <name>selected</name>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
	    <attribute>
	        <name>headKey</name>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
	    <attribute>
	        <name>headValue</name>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
  	</tag>

助手类代码

要注意其中反射我用了jar包,可以自己写反射;

package com.rong.tag;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

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

import org.apache.commons.beanutils.PropertyUtils;

public class SelectTag extends BodyTagSupport {

	private List<Object> items;
	private String name;
	private String valueKey;
	private String textKey;
	private String cssStyle;
	private String headKey;
	private String headValue;
	private String selected;//默认选中
	
	
	public String getSelected() {
		return selected;
	}
	public void setSelected(String selected) {
		this.selected = selected;
	}
	public String getHeadKey() {
		return headKey;
	}
	public void setHeadKey(String headKey) {
		this.headKey = headKey;
	}
	public String getHeadValue() {
		return headValue;
	}
	public void setHeadValue(String headValue) {
		this.headValue = headValue;
	}
	public String getCssStyle() {
		return cssStyle;
	}
	public void setCssStyle(String cssStyle) {
		this.cssStyle = cssStyle;
	}
	public List<Object> getItems() {
		return items;
	}
	public void setItems(List<Object> items) {
		this.items = items;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getValueKey() {
		return valueKey;
	}
	public void setValueKey(String valueKey) {
		this.valueKey = valueKey;
	}
	public String getTextKey() {
		return textKey;
	}
	public void setTextKey(String textKey) {
		this.textKey = textKey;
	}
	
	@Override
	public int doStartTag() throws JspException {
		JspWriter out=pageContext.getOut();
		try {
			String html=toHtml();
			out.println(html);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return super.doStartTag();
	}
	
	private String toHtml() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		StringBuffer sb=new StringBuffer();
		sb.append("");
		sb.append("");
		
		return sb.toString();
	}
	
}

总结

这是俩个最难的自定义标签,不要绕晕就可以了。谢谢浏览。
拜!

你可能感兴趣的:(自定义标签探究(二))