View Javadoc

1   /*
2    * $Id: Select.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * &lt;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   * /&gt;
53   *
54   * &lt;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   * /&gt;
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 }