1 /* 2 * Copyright 2002-2010 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.web.servlet.tags.form; 18 19 import javax.servlet.jsp.JspException; 20 21 /** 22 * Base class for databinding-aware JSP tags that render HTML form input element. 23 * 24 * <p>Provides a set of properties corresponding to the set of HTML attributes 25 * that are common across form input elements. 26 * 27 * @author Rob Harrop 28 * @author Rick Evans 29 * @author Juergen Hoeller 30 * @since 2.0 31 */ 32 public abstract class AbstractHtmlInputElementTag extends AbstractHtmlElementTag { 33 34 /** 35 * The name of the '<code>onfocus</code>' attribute. 36 */ 37 public static final String ONFOCUS_ATTRIBUTE = "onfocus"; 38 39 /** 40 * The name of the '<code>onblur</code>' attribute. 41 */ 42 public static final String ONBLUR_ATTRIBUTE = "onblur"; 43 44 /** 45 * The name of the '<code>onchange</code>' attribute. 46 */ 47 public static final String ONCHANGE_ATTRIBUTE = "onchange"; 48 49 /** 50 * The name of the '<code>accesskey</code>' attribute. 51 */ 52 public static final String ACCESSKEY_ATTRIBUTE = "accesskey"; 53 54 /** 55 * The name of the '<code>disabled</code>' attribute. 56 */ 57 public static final String DISABLED_ATTRIBUTE = "disabled"; 58 59 /** 60 * The name of the '<code>readonly</code>' attribute. 61 */ 62 public static final String READONLY_ATTRIBUTE = "readonly"; 63 64 65 private String onfocus; 66 67 private String onblur; 68 69 private String onchange; 70 71 private String accesskey; 72 73 private String disabled; 74 75 private String readonly; 76 77 78 /** 79 * Set the value of the '<code>onfocus</code>' attribute. 80 * May be a runtime expression. 81 */ setOnfocus(String onfocus)82 public void setOnfocus(String onfocus) { 83 this.onfocus = onfocus; 84 } 85 86 /** 87 * Get the value of the '<code>onfocus</code>' attribute. 88 */ getOnfocus()89 protected String getOnfocus() { 90 return this.onfocus; 91 } 92 93 /** 94 * Set the value of the '<code>onblur</code>' attribute. 95 * May be a runtime expression. 96 */ setOnblur(String onblur)97 public void setOnblur(String onblur) { 98 this.onblur = onblur; 99 } 100 101 /** 102 * Get the value of the '<code>onblur</code>' attribute. 103 */ getOnblur()104 protected String getOnblur() { 105 return this.onblur; 106 } 107 108 /** 109 * Set the value of the '<code>onchange</code>' attribute. 110 * May be a runtime expression. 111 */ setOnchange(String onchange)112 public void setOnchange(String onchange) { 113 this.onchange = onchange; 114 } 115 116 /** 117 * Get the value of the '<code>onchange</code>' attribute. 118 */ getOnchange()119 protected String getOnchange() { 120 return this.onchange; 121 } 122 123 /** 124 * Set the value of the '<code>accesskey</code>' attribute. 125 * May be a runtime expression. 126 */ setAccesskey(String accesskey)127 public void setAccesskey(String accesskey) { 128 this.accesskey = accesskey; 129 } 130 131 /** 132 * Get the value of the '<code>accesskey</code>' attribute. 133 */ getAccesskey()134 protected String getAccesskey() { 135 return this.accesskey; 136 } 137 138 /** 139 * Set the value of the '<code>disabled</code>' attribute. 140 * May be a runtime expression. 141 */ setDisabled(String disabled)142 public void setDisabled(String disabled) { 143 this.disabled = disabled; 144 } 145 146 /** 147 * Get the value of the '<code>disabled</code>' attribute. 148 */ getDisabled()149 protected String getDisabled() { 150 return this.disabled; 151 } 152 153 /** 154 * Sets the value of the '<code>readonly</code>' attribute. 155 * May be a runtime expression. 156 * @see #isReadonly() 157 */ setReadonly(String readonly)158 public void setReadonly(String readonly) { 159 this.readonly = readonly; 160 } 161 162 /** 163 * Gets the value of the '<code>readonly</code>' attribute. 164 * May be a runtime expression. 165 * @see #isReadonly() 166 */ getReadonly()167 protected String getReadonly() { 168 return this.readonly; 169 } 170 171 172 /** 173 * Adds input-specific optional attributes as defined by this base class. 174 */ 175 @Override writeOptionalAttributes(TagWriter tagWriter)176 protected void writeOptionalAttributes(TagWriter tagWriter) throws JspException { 177 super.writeOptionalAttributes(tagWriter); 178 179 writeOptionalAttribute(tagWriter, ONFOCUS_ATTRIBUTE, getOnfocus()); 180 writeOptionalAttribute(tagWriter, ONBLUR_ATTRIBUTE, getOnblur()); 181 writeOptionalAttribute(tagWriter, ONCHANGE_ATTRIBUTE, getOnchange()); 182 writeOptionalAttribute(tagWriter, ACCESSKEY_ATTRIBUTE, getAccesskey()); 183 if (isDisabled()) { 184 tagWriter.writeAttribute(DISABLED_ATTRIBUTE, "disabled"); 185 } 186 if (isReadonly()) { 187 writeOptionalAttribute(tagWriter, READONLY_ATTRIBUTE, "readonly"); 188 } 189 } 190 191 /** 192 * Is the current HTML tag disabled? 193 */ isDisabled()194 protected boolean isDisabled() throws JspException { 195 return evaluateBoolean(DISABLED_ATTRIBUTE, getDisabled()); 196 } 197 198 /** 199 * Is the current HTML tag readonly? 200 * <p>Note: some {@link AbstractHtmlInputElementTag} subclasses (such a those 201 * for checkboxes and radiobuttons) may contain readonly attributes, but are 202 * not affected by them since their values don't change (only their status does.) 203 */ isReadonly()204 protected boolean isReadonly() throws JspException { 205 return evaluateBoolean(READONLY_ATTRIBUTE, getReadonly()); 206 } 207 208 } 209