dbselecttag.java

来自「这是一个轻便的j2ee的web应用框架,是一个在多个项目中运用的实际框架,采用s」· Java 代码 · 共 143 行

JAVA
143
字号
/*
 * Created on 2004-8-22
 *
 */
package com.esimple.framework.web.taglib.database;

import java.util.List;

import javax.servlet.jsp.JspException;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.util.ResponseUtils;

/**
 * @author steven
 *
 */
public class DBSelectTag extends BaseBindingTag {
	protected String multiple = null;
	private String nullLabel;
	
	public void  setNullLabel(String nullLabel){
		this.nullLabel = nullLabel;
	}
	public String getNullLabel(){
		return this.nullLabel;
	}	
	public String getMultiple() {
		return (this.multiple);
	}

	public void setMultiple(String multiple) {
		this.multiple = multiple;
	}
	
	protected String size = null;

	public String getSize() {
		return (this.size);
	}

	public void setSize(String size) {
		this.size = size;
	}

	protected String options = null;

	public String getOptions() {
		return (this.options);
	}

	public void setOptions(String options) {
		this.options = options;
	}
		
	public int doStartTag() throws JspException {
		List  list = getData();
		ResponseUtils.write(pageContext, renderSelectStartElement(list));
		return (EVAL_BODY_TAG);
	 }

	 /**
	  * Create an appropriate select start element based on our parameters.
	  * @throws JspException
	  * @since Struts 1.1
	  */
	 protected String renderSelectStartElement(List list) throws JspException {
		
		 StringBuffer results = new StringBuffer("<select");
        
		 results.append(" name=\"");

		 results.append(name);
		 results.append("\"");
		 if (accesskey != null) {
			 results.append(" accesskey=\"");
			 results.append(accesskey);
			 results.append("\"");
		 }
		 if (multiple != null) {
			 results.append(" multiple=\"multiple\"");
		 }
		 if (size != null) {
			 results.append(" size=\"");
			 results.append(size);
			 results.append("\"");
		 }
		 if (tabindex != null) {
			 results.append(" tabindex=\"");
			 results.append(tabindex);
			 results.append("\"");
		 }
		 results.append(prepareEventHandlers());
		 results.append(prepareStyles());
		 results.append(">\n");
		 
		 if( nullLabel!= null ){
			results.append("<option value=\"\">"+nullLabel +"</option>\n");
		 }
		 if( list != null ){
		 	for( int i=0;i<list.size();i++){
		 		Object vb = list.get(i);
		 		String name=null;
				String value=null;
				try {
					name = BeanUtils.getProperty(vb, labelName);
					value =  BeanUtils.getProperty(vb,valueName);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				if( name == null || value==null ){
					continue;
				}
				
				results.append("   <option value=\"");
				results.append(value);
				results.append("\"");
				
				if( isDefault(value) ){
					results.append(" selected");	
				}
				
				results.append(">");
				results.append(name);
				results.append("</option>\n"); 		
		 	}//for end
		 }
		 return results.toString();
	 }

	public int doEndTag() throws JspException {
		StringBuffer results = new StringBuffer();
		if ( options != null) {
			results.append(options);
		}
		results.append("</select>");

		ResponseUtils.write(pageContext, results.toString());

		return (EVAL_PAGE);
	}
}

⌨️ 快捷键说明

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