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.lang.Nullable;
022
023/**
024 * Base class for databinding-aware JSP tags that render HTML form input element.
025 *
026 * <p>Provides a set of properties corresponding to the set of HTML attributes
027 * that are common across form input elements.
028 *
029 * @author Rob Harrop
030 * @author Rick Evans
031 * @author Juergen Hoeller
032 * @since 2.0
033 */
034@SuppressWarnings("serial")
035public abstract class AbstractHtmlInputElementTag extends AbstractHtmlElementTag {
036
037        /**
038         * The name of the '{@code onfocus}' attribute.
039         */
040        public static final String ONFOCUS_ATTRIBUTE = "onfocus";
041
042        /**
043         * The name of the '{@code onblur}' attribute.
044         */
045        public static final String ONBLUR_ATTRIBUTE = "onblur";
046
047        /**
048         * The name of the '{@code onchange}' attribute.
049         */
050        public static final String ONCHANGE_ATTRIBUTE = "onchange";
051
052        /**
053         * The name of the '{@code accesskey}' attribute.
054         */
055        public static final String ACCESSKEY_ATTRIBUTE = "accesskey";
056
057        /**
058         * The name of the '{@code disabled}' attribute.
059         */
060        public static final String DISABLED_ATTRIBUTE = "disabled";
061
062        /**
063         * The name of the '{@code readonly}' attribute.
064         */
065        public static final String READONLY_ATTRIBUTE = "readonly";
066
067
068        @Nullable
069        private String onfocus;
070
071        @Nullable
072        private String onblur;
073
074        @Nullable
075        private String onchange;
076
077        @Nullable
078        private String accesskey;
079
080        private boolean disabled;
081
082        private boolean readonly;
083
084
085        /**
086         * Set the value of the '{@code onfocus}' attribute.
087         * May be a runtime expression.
088         */
089        public void setOnfocus(String onfocus) {
090                this.onfocus = onfocus;
091        }
092
093        /**
094         * Get the value of the '{@code onfocus}' attribute.
095         */
096        @Nullable
097        protected String getOnfocus() {
098                return this.onfocus;
099        }
100
101        /**
102         * Set the value of the '{@code onblur}' attribute.
103         * May be a runtime expression.
104         */
105        public void setOnblur(String onblur) {
106                this.onblur = onblur;
107        }
108
109        /**
110         * Get the value of the '{@code onblur}' attribute.
111         */
112        @Nullable
113        protected String getOnblur() {
114                return this.onblur;
115        }
116
117        /**
118         * Set the value of the '{@code onchange}' attribute.
119         * May be a runtime expression.
120         */
121        public void setOnchange(String onchange) {
122                this.onchange = onchange;
123        }
124
125        /**
126         * Get the value of the '{@code onchange}' attribute.
127         */
128        @Nullable
129        protected String getOnchange() {
130                return this.onchange;
131        }
132
133        /**
134         * Set the value of the '{@code accesskey}' attribute.
135         * May be a runtime expression.
136         */
137        public void setAccesskey(String accesskey) {
138                this.accesskey = accesskey;
139        }
140
141        /**
142         * Get the value of the '{@code accesskey}' attribute.
143         */
144        @Nullable
145        protected String getAccesskey() {
146                return this.accesskey;
147        }
148
149        /**
150         * Set the value of the '{@code disabled}' attribute.
151         */
152        public void setDisabled(boolean disabled) {
153                this.disabled = disabled;
154        }
155
156        /**
157         * Get the value of the '{@code disabled}' attribute.
158         */
159        protected boolean isDisabled() {
160                return this.disabled;
161        }
162
163        /**
164         * Sets the value of the '{@code readonly}' attribute.
165         */
166        public void setReadonly(boolean readonly) {
167                this.readonly = readonly;
168        }
169
170        /**
171         * Gets the value of the '{@code readonly}' attribute.
172         */
173        protected boolean isReadonly() {
174                return this.readonly;
175        }
176
177
178        /**
179         * Adds input-specific optional attributes as defined by this base class.
180         */
181        @Override
182        protected void writeOptionalAttributes(TagWriter tagWriter) throws JspException {
183                super.writeOptionalAttributes(tagWriter);
184
185                writeOptionalAttribute(tagWriter, ONFOCUS_ATTRIBUTE, getOnfocus());
186                writeOptionalAttribute(tagWriter, ONBLUR_ATTRIBUTE, getOnblur());
187                writeOptionalAttribute(tagWriter, ONCHANGE_ATTRIBUTE, getOnchange());
188                writeOptionalAttribute(tagWriter, ACCESSKEY_ATTRIBUTE, getAccesskey());
189                if (isDisabled()) {
190                        tagWriter.writeAttribute(DISABLED_ATTRIBUTE, "disabled");
191                }
192                if (isReadonly()) {
193                        writeOptionalAttribute(tagWriter, READONLY_ATTRIBUTE, "readonly");
194                }
195        }
196
197}