组装select,根据页面传值组装成select并根据当前直选中某个option

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

import org.apache.log4j.Logger;

import com.sinovatech.cms.common.util.StringUtils;

/**
 * 组装select,根据页面传值组装成select并根据当前直选中某个option
 */
public class ComboSelectTag extends BodyTagSupport {
	private static final long serialVersionUID = -6891364924770628684L;
	public Logger log = Logger.getLogger(this.getClass());
	/**select的id属性*/
	private String id;
	/**select的name属性*/
	private String name;
	/**select的cssClass属性*/
	private String cssClass;
	/**select的styleCss属性*/
	private String styleCss;
	/**select的multiple属性*/
	private String multiple;
	/**select的onChange属性*/
	private String onChange;
	/**option的values*/
	private String optionValues;
	/**option的显示字段*/
	private String displays;
	private String split = ",";
	/**option的当前值*/
	private String currentValue;
	/**option的默认显示*/
	private String defaultDisplay = "全部";
	/**option的默认值*/
	private String defaultValue = "";
	public int doEndTag() throws JspException {
		StringBuffer sb = new StringBuffer("<select");
		PageContext pc = super.pageContext;
		try {
			if(!StringUtils.isBlank(id)){
				sb.append(" id=\"" + id + "\" ");
			}
			if(!StringUtils.isBlank(name)){
				sb.append(" name=\"" + name + "\" ");
			}
			if(!StringUtils.isBlank(cssClass)){
				sb.append("class=\"" + cssClass + "\" ");
			}
			if(!StringUtils.isBlank(styleCss)){
				sb.append("style=\""+styleCss+"\" ");
			}
			if (!StringUtils.isBlank(multiple)){
				sb.append(" multiple=\"" + multiple + "\"");
			}
			if(!StringUtils.isBlank(onChange)){
				sb.append("onchange=\"" + onChange + "\"");
			}
			sb.append(">");
			if(defaultValue==null){
				defaultValue = "";
			}
			if(!StringUtils.isBlank(defaultDisplay)){
				sb.append("<option value = \""+defaultValue+"\">"+defaultDisplay+"</option>");
			}
			String[] values = optionValues.replaceAll(",",split).split(split);
			String[] options = displays.replaceAll(",",split).split(split);
			if(values.length == options.length){
				for (int i=0;i<values.length;i++) {
					String selected = " ";
					String value = values[i];
					if(currentValue != null && (currentValue.equals(value))){
						selected = "selected";
					}
					sb.append("<option value=\"" + value.toString().trim() + "\" "+ selected + ">")
					.append(options[i].toString())
					.append("</option>");
				}
			}else{
				throw new Exception("option和value的数量不匹配!");
			}
			sb.append("</select>");
			JspWriter out = pc.getOut();
			out.print(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
			log.error("select标签输出出错!",e);
		}
		return SKIP_BODY;
	} 
	public Logger getLog() {
		return log;
	}
	public void setLog(Logger log) {
		this.log = log;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCssClass() {
		return cssClass;
	}
	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}
	public String getStyleCss() {
		return styleCss;
	}
	public void setStyleCss(String styleCss) {
		this.styleCss = styleCss;
	}
	public String getMultiple() {
		return multiple;
	}
	public void setMultiple(String multiple) {
		this.multiple = multiple;
	}
	public String getOnChange() {
		return onChange;
	}
	public void setOnChange(String onChange) {
		this.onChange = onChange;
	}
	public String getCurrentValue() {
		return currentValue;
	}
	public void setCurrentValue(String currentValue) {
		this.currentValue = currentValue;
	}
	public String getDefaultDisplay() {
		return defaultDisplay;
	}
	public void setDefaultDisplay(String defaultDisplay) {
		this.defaultDisplay = defaultDisplay;
	}
	public String getDefaultValue() {
		return defaultValue;
	}
	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}
	public String getOptionValues() {
		return optionValues;
	}
	public void setOptionValues(String optionValues) {
		this.optionValues = optionValues;
	}
	public String getDisplays() {
		return displays;
	}
	public void setDisplays(String displays) {
		this.displays = displays;
	}
	public String getSplit() {
		return split;
	}
	public void setSplit(String split) {
		this.split = split;
	}
	
}
调用方法如下:
<mm:comboSelect displays="上,中,下" optionValues="1,2,3" name="xx" currentValue="1" />

你可能感兴趣的:(java,jsp,自定义标签,组合框,select标签)