1 21 package net.mlw.vlh.web.tag; 22 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import javax.servlet.jsp.JspException ; 29 import javax.servlet.jsp.tagext.BodyTagSupport ; 30 31 import net.mlw.vlh.ValueList; 32 import net.mlw.vlh.web.util.JspUtils; 33 34 import org.apache.commons.beanutils.BeanUtils; 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 44 public class DefaultSelectTag extends BodyTagSupport 45 { 46 47 private static final Log LOGGER = LogFactory.getLog(DefaultSelectTag.class); 48 49 private String name; 50 51 private String attributes; 52 53 private String value; 54 55 private String text; 56 57 60 public int doEndTag() throws JspException 61 { 62 ValueListSpaceTag rootTag = (ValueListSpaceTag) JspUtils.getParent(this, ValueListSpaceTag.class); 63 64 ValueList valueList = rootTag.getValueList(); 65 66 StringBuffer sb = new StringBuffer (); 67 68 sb.append("<select name=").append("'").append(name).append("'").append(attributes).append(">"); 69 70 if (bodyContent != null && bodyContent.getString() != null) 71 { 72 sb.append(bodyContent.getString()); 73 } 74 75 try 76 { 77 String [] svalues = pageContext.getRequest().getParameterValues(name); 78 List values = (svalues == null) ? Collections.EMPTY_LIST : Arrays.asList(svalues); 79 80 for (Iterator iter = valueList.getList().iterator(); iter.hasNext();) 81 { 82 Object bean = iter.next(); 83 String value = BeanUtils.getProperty(bean, this.value); 84 85 sb.append("<option "); 86 if (values.contains(value)) 87 { 88 sb.append("selected='true' "); 89 } 90 sb.append("value='").append(value).append("'>"); 91 sb.append(BeanUtils.getProperty(bean, text)); 92 sb.append("</option>"); 93 } 94 } 95 catch (Exception e) 96 { 97 LOGGER.error("DefaultSelectTag.doEndTag() exception...", e); 98 throw new JspException (e); 99 } 100 sb.append("</select>"); 101 102 JspUtils.write(pageContext, sb.toString()); 103 104 release(); 105 106 return EVAL_PAGE; 107 } 108 109 112 public String getAttributes() 113 { 114 return attributes; 115 } 116 117 121 public void setAttributes(String attributes) 122 { 123 this.attributes = attributes; 124 } 125 126 129 public String getName() 130 { 131 return name; 132 } 133 134 138 public void setName(String name) 139 { 140 this.name = name; 141 } 142 143 146 public String getText() 147 { 148 return text; 149 } 150 151 155 public void setText(String text) 156 { 157 this.text = text; 158 } 159 160 163 public String getValue() 164 { 165 return value; 166 } 167 168 172 public void setValue(String value) 173 { 174 this.value = value; 175 } 176 177 private void reset() 178 { 179 this.attributes = null; 180 this.name = null; 181 this.text = null; 182 this.value = null; 183 } 184 185 193 public void release() 194 { 195 super.release(); 196 reset(); 197 } 198 } | Popular Tags |