📄 optionstag.java
字号:
StringBuffer sb = new StringBuffer();
// If a collection was specified, use that mode to render options
if (collection != null) {
Iterator collIterator = getIterator(collection, null);
while (collIterator.hasNext()) {
Object bean = collIterator.next();
Object value = null;
Object label = null;
try {
value = PropertyUtils.getProperty(bean, property);
if (value == null) {
value = "";
}
} catch (IllegalAccessException e) {
throw new JspException(
messages.getMessage("getter.access", property, collection));
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
throw new JspException(
messages.getMessage("getter.result", property, t.toString()));
} catch (NoSuchMethodException e) {
throw new JspException(
messages.getMessage("getter.method", property, collection));
}
try {
if (labelProperty != null) {
label = PropertyUtils.getProperty(bean, labelProperty);
} else {
label = value;
}
if (label == null) {
label = "";
}
} catch (IllegalAccessException e) {
throw new JspException(
messages.getMessage("getter.access", labelProperty, collection));
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
throw new JspException(
messages.getMessage("getter.result", labelProperty, t.toString()));
} catch (NoSuchMethodException e) {
throw new JspException(
messages.getMessage("getter.method", labelProperty, collection));
}
String stringValue = value.toString();
addOption(sb, stringValue, label.toString(), selectTag.isMatched(stringValue));
}
}
// Otherwise, use the separate iterators mode to render options
else {
// Construct iterators for the values and labels collections
Iterator valuesIterator = getIterator(name, property);
Iterator labelsIterator = null;
if ((labelName == null) && (labelProperty == null)) {
labelsIterator = getIterator(name, property); // Same coll.
} else {
labelsIterator = getIterator(labelName, labelProperty);
}
// Render the options tags for each element of the values coll.
while (valuesIterator.hasNext()) {
Object valueObject = valuesIterator.next();
if (valueObject == null) {
valueObject = "";
}
String value = valueObject.toString();
String label = value;
if (labelsIterator.hasNext()) {
Object labelObject = labelsIterator.next();
if (labelObject == null) {
labelObject = "";
}
label = labelObject.toString();
}
addOption(sb, value, label, selectTag.isMatched(value));
}
}
// Render this element to our writer
ResponseUtils.write(pageContext, sb.toString());
// Evaluate the remainder of this page
return EVAL_PAGE;
}
/**
* Release any acquired resources.
*/
public void release() {
super.release();
collection = null;
filter = true;
labelName = null;
labelProperty = null;
name = null;
property = null;
style = null;
styleClass = null;
}
// ------------------------------------------------------ Protected Methods
/**
* Add an option element to the specified StringBuffer based on the
* specified parameters.
*<p>
* Note that this tag specifically does not support the
* <code>styleId</code> tag attribute, which causes the HTML
* <code>id</code> attribute to be emitted. This is because the HTML
* specification states that all "id" attributes in a document have to be
* unique. This tag will likely generate more than one <code>option</code>
* element element, but it cannot use the same <code>id</code> value. It's
* conceivable some sort of mechanism to supply an array of <code>id</code>
* values could be devised, but that doesn't seem to be worth the trouble.
*
* @param sb StringBuffer accumulating our results
* @param value Value to be returned to the server for this option
* @param label Value to be shown to the user for this option
* @param matched Should this value be marked as selected?
*/
protected void addOption(StringBuffer sb, String value, String label, boolean matched) {
sb.append("<option value=\"");
sb.append(value);
sb.append("\"");
if (matched) {
sb.append(" selected=\"selected\"");
}
if (style != null) {
sb.append(" style=\"");
sb.append(style);
sb.append("\"");
}
if (styleClass != null) {
sb.append(" class=\"");
sb.append(styleClass);
sb.append("\"");
}
sb.append(">");
if (filter) {
sb.append(ResponseUtils.filter(label));
} else {
sb.append(label);
}
sb.append("</option>\r\n");
}
/**
* Return an iterator for the option labels or values, based on our
* configured properties.
*
* @param name Name of the bean attribute (if any)
* @param property Name of the bean property (if any)
*
* @exception JspException if an error occurs
*/
protected Iterator getIterator(String name, String property) throws JspException {
// Identify the bean containing our collection
String beanName = name;
if (beanName == null) {
beanName = Constants.BEAN_KEY;
}
Object bean = RequestUtils.lookup(pageContext, beanName, null);
if (bean == null) {
throw new JspException(messages.getMessage("getter.bean", beanName));
}
// Identify the collection itself
Object collection = bean;
if (property != null) {
try {
collection = PropertyUtils.getProperty(bean, property);
if (collection == null) {
throw new JspException(messages.getMessage("getter.property", property));
}
} catch (IllegalAccessException e) {
throw new JspException(messages.getMessage("getter.access", property, name));
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
throw new JspException(
messages.getMessage("getter.result", property, t.toString()));
} catch (NoSuchMethodException e) {
throw new JspException(messages.getMessage("getter.method", property, name));
}
}
// Construct and return an appropriate iterator
if (collection.getClass().isArray()) {
collection = Arrays.asList((Object[]) collection);
}
if (collection instanceof Collection) {
return (((Collection) collection).iterator());
} else if (collection instanceof Iterator) {
return ((Iterator) collection);
} else if (collection instanceof Map) {
return (((Map) collection).entrySet().iterator());
} else if (collection instanceof Enumeration) {
return IteratorUtils.asIterator((Enumeration) collection);
} else {
throw new JspException(
messages.getMessage("optionsTag.iterator", collection.toString()));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -