001/*
002 * Copyright 2002-2013 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
021/**
022 * Data-binding aware JSP tag for rendering a hidden HTML '{@code input}' field
023 * containing the databound value.
024 *
025 * <p>Example (binding to 'name' property of form backing object):
026 * <pre class="code>
027 * &lt;form:hidden path=&quot;name&quot;/&gt;
028 * </pre>
029 *
030 * @author Rob Harrop
031 * @author Juergen Hoeller
032 * @author Rossen Stoyanchev
033 * @since 2.0
034 */
035@SuppressWarnings("serial")
036public class HiddenInputTag extends AbstractHtmlElementTag {
037
038        /**
039         * The name of the '{@code disabled}' attribute.
040         */
041        public static final String DISABLED_ATTRIBUTE = "disabled";
042
043        private boolean disabled;
044
045
046        /**
047         * Set the value of the '{@code disabled}' attribute.
048         * May be a runtime expression.
049         */
050        public void setDisabled(boolean disabled) {
051                this.disabled = disabled;
052        }
053
054        /**
055         * Get the value of the '{@code disabled}' attribute.
056         */
057        public boolean isDisabled() {
058                return this.disabled;
059        }
060
061
062        /**
063         * Flags "type" as an illegal dynamic attribute.
064         */
065        @Override
066        protected boolean isValidDynamicAttribute(String localName, Object value) {
067                return !"type".equals(localName);
068        }
069
070        /**
071         * Writes the HTML '{@code input}' tag to the supplied {@link TagWriter} including the
072         * databound value.
073         * @see #writeDefaultAttributes(TagWriter)
074         * @see #getBoundValue()
075         */
076        @Override
077        protected int writeTagContent(TagWriter tagWriter) throws JspException {
078                tagWriter.startTag("input");
079                writeDefaultAttributes(tagWriter);
080                tagWriter.writeAttribute("type", "hidden");
081                if (isDisabled()) {
082                        tagWriter.writeAttribute(DISABLED_ATTRIBUTE, "disabled");
083                }
084                String value = getDisplayString(getBoundValue(), getPropertyEditor());
085                tagWriter.writeAttribute("value", processFieldValue(getName(), value, "hidden"));
086                tagWriter.endTag();
087                return SKIP_BODY;
088        }
089
090}