`
kevinhrw
  • 浏览: 187946 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

自己写的SelectTag

    博客分类:
  • java
阅读更多
如果你没有spring的tag或struct的tag,使用我这个做下拉选单,还是很方便的.
<iit:select name= "selectName" items="${selectList}" itemValue = "selectValue" itemLabel = "selectLabel" selectValue="${selectValue}"/>

一个select下拉选单,一行搞定.
可以带onchange事件,带默认值选定
自己觉得使用很方便.

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

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

import org.apache.commons.beanutils.BeanUtils;

public class SelectTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9049765299272180960L;

	/**
	 * The {@link Collection}, {@link Map} or array of objects used to generate
	 * the inner '<code>option</code>' tags.
	 */
	private Object items;

	/**
	 * The name of the property mapped to the '<code>value</code>' attribute
	 * of the '<code>option</code>' tag.
	 */
	private String itemValue;

	/**
	 * The name of the property mapped to the inner text of the '<code>option</code>'
	 * tag.
	 */
	private String itemLabel;
	private String selectValue;
	private String emptyValue;
	private String name;
	private String id;

	private String onchange;

	public void release() {
		items = null;
		itemValue = null;
		itemLabel = null;
		selectValue = null;
		emptyValue = null;
		name = null;
		id = null;
		onchange = null;
	}

	@SuppressWarnings("unchecked")
	public int doEndTag() throws JspException {
		JspWriter jspOut = pageContext.getOut();
		try {
			jspOut.append("<select id=\"" + getId() + "\" name=\"" + name + "\" " + getOnchange() + " >");
			jspOut.append("<option value=\"" + getEmptyValue() + "\">Please Select</option>");
			if (items != null) {
				Collection itemsList = (Collection) items;
				for (Iterator iterator = itemsList.iterator(); iterator.hasNext();) {
					Object obj = (Object) iterator.next();
					Object value = BeanUtils.getProperty(obj, itemValue);
					Object label = BeanUtils.getProperty(obj, itemLabel);
					if (value != null && value.equals(selectValue)) {
						jspOut.append("<option value=\"" + value + "\" selected = \"selected\">" + label + "</option>");
					} else {
						jspOut.append("<option value=\"" + value + "\">" + label + "</option>");
					}

				}
			}
			jspOut.append("</select>");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			itemValue = null;
			itemLabel = null;
			selectValue = null;
			emptyValue = null;
			name = null;
			id = null;
			onchange = null;
		}

		return EVAL_PAGE;
	}

	public String getId() {
		if (id == null) {
			id = name;
		}
		return id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setItems(Object items) {
		this.items = items;
	}

	public void setItemValue(String itemValue) {
		this.itemValue = itemValue;
	}

	public void setItemLabel(String itemLabel) {
		this.itemLabel = itemLabel;
	}

	public void setSelectValue(String selectValue) {
		this.selectValue = selectValue;
	}

	public void setEmptyValue(String emptyValue) {
		this.emptyValue = emptyValue;
	}

	public String getEmptyValue() {
		if (emptyValue == null) {
			emptyValue = "-1";
		}
		return emptyValue;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public String getOnchange() {
		if (onchange == null) {
			return "";
		}
		return "onchange=\"" + onchange + "\"";

	}
}


           <tag>
		<name>select</name>
		<tagclass>com...SelectTag</tagclass>
		<bodycontent>empty</bodycontent>
		<attribute>
			<name>name</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>id</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>items</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>itemValue</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>itemLabel</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>

		<attribute>
			<name>emptyValue</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>

		<attribute>
			<name>selectValue</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		
		<attribute>
			<name>onchange</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>
分享到:
评论

相关推荐

    自适应宽度的标签导航

    &lt;LI class=selectTag&gt;&lt;A onClick="selectTag('tagContent1',this)" href="javascript:void(0)"&gt;标签二&lt;/A&gt; &lt;LI&gt;&lt;A onClick="selectTag('tagContent2',this)" href="javascript:void(0)"&gt;自适应宽度的标签&lt;/A&gt;...

    JQUERY滑动门最简单代码

    $("#tags_op li.selectTag").removeClass("selectTag"); $(this).addClass("selectTag"); $("#tagContent &gt; div:visible").hide(); $("#tagContent div:eq(" + index + ")").show(); }) }) }) [removed] 大大...

    vue-tag-selector-类似标签的输入。 轻巧,可自定义并处理REGEX验证!-Vue.js开发

    Vue-tag-selector是用于vuejs的标签类型字段的组件。 轻巧(压缩后为6.3kb),并且可以自定义。 提供正则表达式验证。 查看演示I Vue-tag-selector是用于vuejs的标签类型字段的组件。 轻巧(压缩后为6.3kb),并且...

    javascript Select标记中options操作方法集合

    javascript操作Select标记中options集合 先来看看options集合的这几个方法: ... javascript代码如下: var selectTag = null; //select标记 var OPTONLENGTH = 10; //每次填充option数 var colls = []; //对select标

    htmlparser

    org.htmlparser.tags.SelectTag.class org.htmlparser.tags.Span.class org.htmlparser.tags.StyleTag.class org.htmlparser.tags.TableColumn.class org.htmlparser.tags.TableHeader.class org.htmlparser.tags....

    HTML Parser

    org.htmlparser.tags.SelectTag org.htmlparser.util.Translate org.htmlparser.util.sort.Sort org.htmlparser.beans.BeanyBaby org.htmlparser.http.HttpHeader org.htmlparser.lexer.PageIndex org.htmlparser....

    SPRING API 2.0.CHM

    All Classes AbstractAdvisorAutoProxyCreator AbstractApplicationContext AbstractApplicationEventMulticaster AbstractAspectJAdvice AbstractAspectJAdvisorFactory AbstractAspectJAdvisorFactory....

Global site tag (gtag.js) - Google Analytics