1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.components;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
23 import com.opensymphony.xwork2.util.ValueStack;
24
25 /***
26 * <!-- START SNIPPET: javadoc -->
27 *
28 * Render an HTML input tag of type select.
29 *
30 * <!-- END SNIPPET: javadoc -->
31 *
32 * <p/> <b>Examples</b>
33 * <pre>
34 * <!-- START SNIPPET: example -->
35 *
36 * <s:select label="Pets"
37 * name="petIds"
38 * list="petDao.pets"
39 * listKey="id"
40 * listValue="name"
41 * multiple="true"
42 * size="3"
43 * required="true"
44 * />
45 *
46 * <s:select label="Months"
47 * name="months"
48 * headerKey="-1" headerValue="Select Month"
49 * list="#{'01':'Jan', '02':'Feb', [...]}"
50 * value="selectedMonth"
51 * required="true"
52 * />
53 *
54 * // The month id (01, 02, ...) returned by the getSelectedMonth() call
55 * // against the stack will be auto-selected
56 *
57 * <!-- END SNIPPET: example -->
58 * </pre>
59 *
60 * <p/>
61 *
62 * <!-- START SNIPPET: exnote -->
63 *
64 * Note: For any of the tags that use lists (select probably being the most ubiquitous), which uses the OGNL list
65 * notation (see the "months" example above), it should be noted that the map key created (in the months example,
66 * the '01', '02', etc.) is typed. '1' is a char, '01' is a String, "1" is a String. This is important since if
67 * the value returned by your "value" attribute is NOT the same type as the key in the "list" attribute, they
68 * WILL NOT MATCH, even though their String values may be equivalent. If they don't match, nothing in your list
69 * will be auto-selected.<p/>
70 *
71 * <!-- END SNIPPET: exnote -->
72 *
73 * @s.tag name="select" tld-body-content="JSP" tld-tag-class="org.apache.struts2.views.jsp.ui.SelectTag"
74 * description="Render a select element"
75 */
76 public class Select extends ListUIBean {
77 final public static String TEMPLATE = "select";
78
79 protected String emptyOption;
80 protected String headerKey;
81 protected String headerValue;
82 protected String multiple;
83 protected String size;
84
85 public Select(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
86 super(stack, request, response);
87 }
88
89 protected String getDefaultTemplate() {
90 return TEMPLATE;
91 }
92
93 public void evaluateExtraParams() {
94 super.evaluateExtraParams();
95
96 if (emptyOption != null) {
97 addParameter("emptyOption", findValue(emptyOption, Boolean.class));
98 }
99
100 if (multiple != null) {
101 addParameter("multiple", findValue(multiple, Boolean.class));
102 }
103
104 if (size != null) {
105 addParameter("size", findString(size));
106 }
107
108 if ((headerKey != null) && (headerValue != null)) {
109 addParameter("headerKey", findString(headerKey));
110 addParameter("headerValue", findString(headerValue));
111 }
112 }
113
114 /***
115 * Whether or not to add an empty (--) option after the header option
116 * @s.tagattribute required="false" type="Boolean" default="false"
117 */
118 public void setEmptyOption(String emptyOption) {
119 this.emptyOption = emptyOption;
120 }
121
122 /***
123 * Key for first item in list. Must not be empty! "'-1'" and "''" is correct, "" is bad.
124 * @s.tagattribute required="false"
125 */
126 public void setHeaderKey(String headerKey) {
127 this.headerKey = headerKey;
128 }
129
130 /***
131 * Value expression for first item in list
132 * @s.tagattribute required="false"
133 */
134 public void setHeaderValue(String headerValue) {
135 this.headerValue = headerValue;
136 }
137
138 /***
139 * 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.
140 * @s.tagattribute required="false" type="Boolean" default="false"
141 */
142 public void setMultiple(String multiple) {
143 this.multiple = multiple;
144 }
145
146 /***
147 * Size of the element box (# of elements to show)
148 * @s.tagattribute required="false" type="Integer"
149 */
150 public void setSize(String size) {
151 this.size = size;
152 }
153 }