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