View Javadoc

1   /*
2    * $Id: Select.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * &lt;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   * /&gt;
45   *
46   * &lt;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   * /&gt;
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 }