⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dropdownlisttag.java

📁 企业人力资源管理
💻 JAVA
字号:
package com.y2.hr.human.common;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

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

/**
 * 此标签为模仿ASP.NET的DropDownList控件,绑定数据源,并指定值和显示的文本
 * 
 * @author Rocky
 * 
 */
public class DropDownListTag extends BodyTagSupport {

	// 选项的内容
	private List items;
	// 要显示的文本
	private String text;
	// 选中后获取的值
	private String value;
	// 下拉列表框的ID
	private String id;
	// 为下拉列表框添加的样式
	private String style;
	// 下拉列表框的默认值
	private String selectedValue;
	// 添加默认选项,如:--请选择...--
	private String defaultOption;
	// 添加事件
	private String event;

	public void setSelectedValue(String selectedValue) {
		this.selectedValue = selectedValue;
	}

	public void setDefaultOption(String defaultOption) {
		this.defaultOption = defaultOption;
		// System.out.println("测试:" + defaultOption);
	}

	public void setItems(List items) {
		try {
			this.items = items;
		} catch (Exception e) {
			System.out.println(this.defaultOption + "Exception");
		}
	}

	public void setText(String text) {
		this.text = text;
	}

	public void setValue(String value) {
		this.value = value;
	}

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

	public void setStyle(String style) {
		this.style = style;
	}

	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		JspWriter out = this.bodyContent.getEnclosingWriter();
		StringBuffer buf = new StringBuffer();

		buf.append("\n<select id=\"" + id + "\" name=\"" + id + "\"");
		if (this.style != null) {
			buf.append(" style=\"" + style + "\"");
		}
		if (event != null) {
			buf.append(" " + event);
		}
		buf.append(">");
		if (defaultOption != null) {
			buf.append("<option value=\"NULL\">-请选择" + defaultOption
					+ "-</option>");
		}
		for (int i = 0; i < items.size(); i++) {
			Object obj = items.get(i);
			Method[] methods = obj.getClass().getMethods();
			String tempText = "get" + this.text;
			String tempValue = "get" + this.value;
			String sText = "";
			String sValue = "";
			for (Method m : methods) {
				if (m.getName().toLowerCase().equals(tempText.toLowerCase())) {
					try {
						sText = m.invoke(obj, null).toString();
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if (m.getName().toLowerCase().equals(tempValue.toLowerCase())) {
					try {
						sValue = m.invoke(obj, null).toString();
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			try {
				if (this.selectedValue != null
						&& this.selectedValue.equals(sValue)) {
					buf.append("\n\t <option value=\"" + sValue
							+ "\" selected>" + sText
							+ "</option>                ");
				} else {
					buf.append("\n\t <option value=\"" + sValue + "\">" + sText
							+ "</option>               			");
				}
			} catch (Exception e) {
				buf.append("\n\t <option value=\"" + sValue + "\">" + sText
						+ "</option>               			");
			}
		}
		buf.append("\n</select>");
		try {
			out.print(buf.toString());
			// System.out.println(buf.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return super.doEndTag();
	}

	public void setEvent(String event) {
		this.event = event;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -