001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.servlet.tags.form;
018
019import javax.servlet.jsp.JspException;
020
021import org.springframework.web.bind.WebDataBinder;
022
023/**
024 * The {@code <checkboxes>} tag renders multiple HTML 'input' tags with type 'checkbox'.
025 * Intended to be used with a Collection as the {@link #getItems()} bound value.
026 *
027 * <p>
028 * <table>
029 * <caption>Attribute Summary</caption>
030 * <thead>
031 * <tr>
032 * <th class="colFirst">Attribute</th>
033 * <th class="colOne">Required?</th>
034 * <th class="colOne">Runtime Expression?</th>
035 * <th class="colLast">Description</th>
036 * </tr>
037 * </thead>
038 * <tbody>
039 * <tr class="altColor">
040 * <td><p>accesskey</p></td>
041 * <td><p>false</p></td>
042 * <td><p>true</p></td>
043 * <td><p>HTML Standard Attribute</p></td>
044 * </tr>
045 * <tr class="rowColor">
046 * <td><p>cssClass</p></td>
047 * <td><p>false</p></td>
048 * <td><p>true</p></td>
049 * <td><p>HTML Optional Attribute</p></td>
050 * </tr>
051 * <tr class="altColor">
052 * <td><p>cssErrorClass</p></td>
053 * <td><p>false</p></td>
054 * <td><p>true</p></td>
055 * <td><p>HTML Optional Attribute. Used when the bound field has errors.</p></td>
056 * </tr>
057 * <tr class="rowColor">
058 * <td><p>cssStyle</p></td>
059 * <td><p>false</p></td>
060 * <td><p>true</p></td>
061 * <td><p>HTML Optional Attribute</p></td>
062 * </tr>
063 * <tr class="altColor">
064 * <td><p>delimiter</p></td>
065 * <td><p>false</p></td>
066 * <td><p>true</p></td>
067 * <td><p>Delimiter to use between each 'input' tag with type 'checkbox'.
068 * There is no delimiter by default.</p></td>
069 * </tr>
070 * <tr class="rowColor">
071 * <td><p>dir</p></td>
072 * <td><p>false</p></td>
073 * <td><p>true</p></td>
074 * <td><p>HTML Standard Attribute</p></td>
075 * </tr>
076 * <tr class="altColor">
077 * <td><p>disabled</p></td>
078 * <td><p>false</p></td>
079 * <td><p>true</p></td>
080 * <td><p>HTML Optional Attribute. Setting the value of this attribute to 'true'
081 * will disable the HTML element.</p></td>
082 * </tr>
083 * <tr class="rowColor">
084 * <td><p>element</p></td>
085 * <td><p>false</p></td>
086 * <td><p>true</p></td>
087 * <td><p>Specifies the HTML element that is used to enclose each 'input' tag with
088 * type 'checkbox'. Defaults to 'span'.</p></td>
089 * </tr>
090 * <tr class="altColor">
091 * <td><p>htmlEscape</p></td>
092 * <td><p>false</p></td>
093 * <td><p>true</p></td>
094 * <td><p>Enable/disable HTML escaping of rendered values.</p></td>
095 * </tr>
096 * <tr class="rowColor">
097 * <td><p>id</p></td>
098 * <td><p>false</p></td>
099 * <td><p>true</p></td>
100 * <td><p>HTML Standard Attribute</p></td>
101 * </tr>
102 * <tr class="altColor">
103 * <td><p>itemLabel</p></td>
104 * <td><p>false</p></td>
105 * <td><p>true</p></td>
106 * <td><p>Value to be displayed as part of the 'input' tags with type 'checkbox'</p></td>
107 * </tr>
108 * <tr class="rowColor">
109 * <td><p>items</p></td>
110 * <td><p>true</p></td>
111 * <td><p>true</p></td>
112 * <td><p>The Collection, Map or array of objects used to generate the 'input' tags
113 * with type 'checkbox'</p></td>
114 * </tr>
115 * <tr class="altColor">
116 * <td><p>itemValue</p></td>
117 * <td><p>false</p></td>
118 * <td><p>true</p></td>
119 * <td><p>Name of the property mapped to 'value' attribute of the 'input' tags with
120 * type 'checkbox'</p></td>
121 * </tr>
122 * <tr class="rowColor">
123 * <td><p>lang</p></td>
124 * <td><p>false</p></td>
125 * <td><p>true</p></td>
126 * <td><p>HTML Standard Attribute</p></td>
127 * </tr>
128 * <tr class="altColor">
129 * <td><p>onblur</p></td>
130 * <td><p>false</p></td>
131 * <td><p>true</p></td>
132 * <td><p>HTML Event Attribute</p></td>
133 * </tr>
134 * <tr class="rowColor">
135 * <td><p>onchange</p></td>
136 * <td><p>false</p></td>
137 * <td><p>true</p></td>
138 * <td><p>HTML Event Attribute</p></td>
139 * </tr>
140 * <tr class="altColor">
141 * <td><p>onclick</p></td>
142 * <td><p>false</p></td>
143 * <td><p>true</p></td>
144 * <td><p>HTML Event Attribute</p></td>
145 * </tr>
146 * <tr class="rowColor">
147 * <td><p>ondblclick</p></td>
148 * <td><p>false</p></td>
149 * <td><p>true</p></td>
150 * <td><p>HTML Event Attribute</p></td>
151 * </tr>
152 * <tr class="altColor">
153 * <td><p>onfocus</p></td>
154 * <td><p>false</p></td>
155 * <td><p>true</p></td>
156 * <td><p>HTML Event Attribute</p></td>
157 * </tr>
158 * <tr class="rowColor">
159 * <td><p>onkeydown</p></td>
160 * <td><p>false</p></td>
161 * <td><p>true</p></td>
162 * <td><p>HTML Event Attribute</p></td>
163 * </tr>
164 * <tr class="altColor">
165 * <td><p>onkeypress</p></td>
166 * <td><p>false</p></td>
167 * <td><p>true</p></td>
168 * <td><p>HTML Event Attribute</p></td>
169 * </tr>
170 * <tr class="rowColor">
171 * <td><p>onkeyup</p></td>
172 * <td><p>false</p></td>
173 * <td><p>true</p></td>
174 * <td><p>HTML Event Attribute</p></td>
175 * </tr>
176 * <tr class="altColor">
177 * <td><p>onmousedown</p></td>
178 * <td><p>false</p></td>
179 * <td><p>true</p></td>
180 * <td><p>HTML Event Attribute</p></td>
181 * </tr>
182 * <tr class="rowColor">
183 * <td><p>onmousemove</p></td>
184 * <td><p>false</p></td>
185 * <td><p>true</p></td>
186 * <td><p>HTML Event Attribute</p></td>
187 * </tr>
188 * <tr class="altColor">
189 * <td><p>onmouseout</p></td>
190 * <td><p>false</p></td>
191 * <td><p>true</p></td>
192 * <td><p>HTML Event Attribute</p></td>
193 * </tr>
194 * <tr class="rowColor">
195 * <td><p>onmouseover</p></td>
196 * <td><p>false</p></td>
197 * <td><p>true</p></td>
198 * <td><p>HTML Event Attribute</p></td>
199 * </tr>
200 * <tr class="altColor">
201 * <td><p>onmouseup</p></td>
202 * <td><p>false</p></td>
203 * <td><p>true</p></td>
204 * <td><p>HTML Event Attribute</p></td>
205 * </tr>
206 * <tr class="rowColor">
207 * <td><p>path</p></td>
208 * <td><p>true</p></td>
209 * <td><p>true</p></td>
210 * <td><p>Path to property for data binding</p></td>
211 * </tr>
212 * <tr class="altColor">
213 * <td><p>tabindex</p></td>
214 * <td><p>false</p></td>
215 * <td><p>true</p></td>
216 * <td><p>HTML Standard Attribute</p></td>
217 * </tr>
218 * <tr class="rowColor">
219 * <td><p>title</p></td>
220 * <td><p>false</p></td>
221 * <td><p>true</p></td>
222 * <td><p>HTML Standard Attribute</p></td>
223 * </tr>
224 * </tbody>
225 * </table>
226 *
227 * @author Thomas Risberg
228 * @author Juergen Hoeller
229 * @author Mark Fisher
230 * @since 2.5
231 */
232@SuppressWarnings("serial")
233public class CheckboxesTag extends AbstractMultiCheckedElementTag {
234
235        @Override
236        protected int writeTagContent(TagWriter tagWriter) throws JspException {
237                super.writeTagContent(tagWriter);
238
239                if (!isDisabled()) {
240                        // Write out the 'field was present' marker.
241                        tagWriter.startTag("input");
242                        tagWriter.writeAttribute("type", "hidden");
243                        String name = WebDataBinder.DEFAULT_FIELD_MARKER_PREFIX + getName();
244                        tagWriter.writeAttribute("name", name);
245                        tagWriter.writeAttribute("value", processFieldValue(name, "on", "hidden"));
246                        tagWriter.endTag();
247                }
248
249                return SKIP_BODY;
250        }
251
252        @Override
253        protected String getInputType() {
254                return "checkbox";
255        }
256
257}