001: package com.opensymphony.webwork.components;
002:
003: import com.opensymphony.xwork.util.OgnlValueStack;
004:
005: import javax.servlet.http.HttpServletRequest;
006: import javax.servlet.http.HttpServletResponse;
007:
008: /**
009: * <!-- START SNIPPET: javadoc -->
010: *
011: * Render an HTML input tag of type select.
012: *
013: * <!-- END SNIPPET: javadoc -->
014: *
015: * <p/> <b>Examples</b>
016: * <pre>
017: * <!-- START SNIPPET: example -->
018: *
019: * <ww:select label="Pets"
020: * name="petIds"
021: * list="petDao.pets"
022: * listKey="id"
023: * listValue="name"
024: * multiple="true"
025: * size="3"
026: * required="true"
027: * />
028: *
029: * <ww:select label="Months"
030: * name="months"
031: * headerKey="-1" headerValue="Select Month"
032: * list="#{'01':'Jan', '02':'Feb', [...]}"
033: * value="selectedMonth"
034: * required="true"
035: * />
036: *
037: * // The month id (01, 02, ...) returned by the getSelectedMonth() call
038: * // against the stack will be auto-selected
039: *
040: * <!-- END SNIPPET: example -->
041: * </pre>
042: *
043: * <p/>
044: *
045: * <!-- START SNIPPET: exnote -->
046: *
047: * Note: For any of the tags that use lists (select probably being the most ubiquitous), which uses the OGNL list
048: * notation (see the "months" example above), it should be noted that the map key created (in the months example,
049: * the '01', '02', etc.) is typed. '1' is a char, '01' is a String, "1" is a String. This is important since if
050: * the value returned by your "value" attribute is NOT the same type as the key in the "list" attribute, they
051: * WILL NOT MATCH, even though their String values may be equivalent. If they don't match, nothing in your list
052: * will be auto-selected.<p/>
053: *
054: * <!-- END SNIPPET: exnote -->
055: *
056: * @author Patrick Lightbody
057: * @author Rene Gielen
058: * @author tm_jee
059: * @version $Revision: 2468 $
060: * @since 2.2
061: *
062: * @ww.tag name="select" tld-body-content="JSP" tld-tag-class="com.opensymphony.webwork.views.jsp.ui.SelectTag"
063: * description="Render a select element"
064: */
065: public class Select extends ListUIBean {
066: final public static String TEMPLATE = "select";
067:
068: protected String emptyOption;
069: protected String headerKey;
070: protected String headerValue;
071: protected String multiple;
072: protected String size;
073:
074: public Select(OgnlValueStack stack, HttpServletRequest request,
075: HttpServletResponse response) {
076: super (stack, request, response);
077: }
078:
079: protected String getDefaultTemplate() {
080: return TEMPLATE;
081: }
082:
083: public void evaluateExtraParams() {
084: super .evaluateExtraParams();
085:
086: if (emptyOption != null) {
087: addParameter("emptyOption", findValue(emptyOption,
088: Boolean.class));
089: }
090:
091: if (multiple != null) {
092: addParameter("multiple", findValue(multiple, Boolean.class));
093: }
094:
095: if (size != null) {
096: addParameter("size", findString(size));
097: }
098:
099: if ((headerKey != null) && (headerValue != null)) {
100: addParameter("headerKey", findString(headerKey));
101: addParameter("headerValue", findString(headerValue));
102: }
103: }
104:
105: /**
106: * Whether or not to add an empty (--) option after the header option
107: * @ww.tagattribute required="false" type="Boolean" default="false"
108: */
109: public void setEmptyOption(String emptyOption) {
110: this .emptyOption = emptyOption;
111: }
112:
113: /**
114: * Key for first item in list. Must not be empty! "'-1'" and "''" is correct, "" is bad.
115: * @ww.tagattribute required="false"
116: */
117: public void setHeaderKey(String headerKey) {
118: this .headerKey = headerKey;
119: }
120:
121: /**
122: * Value expression for first item in list
123: * @ww.tagattribute required="false"
124: */
125: public void setHeaderValue(String headerValue) {
126: this .headerValue = headerValue;
127: }
128:
129: /**
130: * Creates a multiple select. The tag will pre-select multiple values if the values are passed as an Array (of appropriate types) via the value attribute. Passing a Collection may work too? Haven't tested this.
131: * @ww.tagattribute required="false" type="Boolean" default="false"
132: */
133: public void setMultiple(String multiple) {
134: this .multiple = multiple;
135: }
136:
137: /**
138: * Size of the element box (# of elements to show)
139: * @ww.tagattribute required="false" type="Integer"
140: */
141: public void setSize(String size) {
142: this.size = size;
143: }
144: }
|