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