001/* 002 * Copyright 2002-2014 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 * The {@code <password>} tag renders an HTML 'input' tag with type 'password' 023 * using the bound value. 024 * 025 * <p> 026 * <table> 027 * <caption>Attribute Summary</caption> 028 * <thead> 029 * <tr> 030 * <th class="colFirst">Attribute</th> 031 * <th class="colOne">Required?</th> 032 * <th class="colOne">Runtime Expression?</th> 033 * <th class="colLast">Description</th> 034 * </tr> 035 * </thead> 036 * <tbody> 037 * <tr class="altColor"> 038 * <td><p>accesskey</p></td> 039 * <td><p>false</p></td> 040 * <td><p>true</p></td> 041 * <td><p>HTML Standard Attribute</p></td> 042 * </tr> 043 * <tr class="rowColor"> 044 * <td><p>alt</p></td> 045 * <td><p>false</p></td> 046 * <td><p>true</p></td> 047 * <td><p>HTML Optional Attribute</p></td> 048 * </tr> 049 * <tr class="altColor"> 050 * <td><p>autocomplete</p></td> 051 * <td><p>false</p></td> 052 * <td><p>true</p></td> 053 * <td><p>Common Optional Attribute</p></td> 054 * </tr> 055 * <tr class="rowColor"> 056 * <td><p>cssClass</p></td> 057 * <td><p>false</p></td> 058 * <td><p>true</p></td> 059 * <td><p>HTML Optional Attribute</p></td> 060 * </tr> 061 * <tr class="altColor"> 062 * <td><p>cssErrorClass</p></td> 063 * <td><p>false</p></td> 064 * <td><p>true</p></td> 065 * <td><p>HTML Optional Attribute. Used when the bound field has 066 * errors.</p></td> 067 * </tr> 068 * <tr class="rowColor"> 069 * <td><p>cssStyle</p></td> 070 * <td><p>false</p></td> 071 * <td><p>true</p></td> 072 * <td><p>HTML Optional Attribute</p></td> 073 * </tr> 074 * <tr class="altColor"> 075 * <td><p>dir</p></td> 076 * <td><p>false</p></td> 077 * <td><p>true</p></td> 078 * <td><p>HTML Standard Attribute</p></td> 079 * </tr> 080 * <tr class="rowColor"> 081 * <td><p>disabled</p></td> 082 * <td><p>false</p></td> 083 * <td><p>true</p></td> 084 * <td><p>HTML Optional Attribute. Setting the value of this attribute to 'true' 085 * will disable the HTML element.</p></td> 086 * </tr> 087 * <tr class="altColor"> 088 * <td><p>htmlEscape</p></td> 089 * <td><p>false</p></td> 090 * <td><p>true</p></td> 091 * <td><p>Enable/disable HTML escaping of rendered values.</p></td> 092 * </tr> 093 * <tr class="rowColor"> 094 * <td><p>id</p></td> 095 * <td><p>false</p></td> 096 * <td><p>true</p></td> 097 * <td><p>HTML Standard Attribute</p></td> 098 * </tr> 099 * <tr class="altColor"> 100 * <td><p>lang</p></td> 101 * <td><p>false</p></td> 102 * <td><p>true</p></td> 103 * <td><p>HTML Standard Attribute</p></td> 104 * </tr> 105 * <tr class="rowColor"> 106 * <td><p>maxlength</p></td> 107 * <td><p>false</p></td> 108 * <td><p>true</p></td> 109 * <td><p>HTML Optional Attribute</p></td> 110 * </tr> 111 * <tr class="altColor"> 112 * <td><p>onblur</p></td> 113 * <td><p>false</p></td> 114 * <td><p>true</p></td> 115 * <td><p>HTML Event Attribute</p></td> 116 * </tr> 117 * <tr class="rowColor"> 118 * <td><p>onchange</p></td> 119 * <td><p>false</p></td> 120 * <td><p>true</p></td> 121 * <td><p>HTML Event Attribute</p></td> 122 * </tr> 123 * <tr class="altColor"> 124 * <td><p>onclick</p></td> 125 * <td><p>false</p></td> 126 * <td><p>true</p></td> 127 * <td><p>HTML Event Attribute</p></td> 128 * </tr> 129 * <tr class="rowColor"> 130 * <td><p>ondblclick</p></td> 131 * <td><p>false</p></td> 132 * <td><p>true</p></td> 133 * <td><p>HTML Event Attribute</p></td> 134 * </tr> 135 * <tr class="altColor"> 136 * <td><p>onfocus</p></td> 137 * <td><p>false</p></td> 138 * <td><p>true</p></td> 139 * <td><p>HTML Event Attribute</p></td> 140 * </tr> 141 * <tr class="rowColor"> 142 * <td><p>onkeydown</p></td> 143 * <td><p>false</p></td> 144 * <td><p>true</p></td> 145 * <td><p>HTML Event Attribute</p></td> 146 * </tr> 147 * <tr class="altColor"> 148 * <td><p>onkeypress</p></td> 149 * <td><p>false</p></td> 150 * <td><p>true</p></td> 151 * <td><p>HTML Event Attribute</p></td> 152 * </tr> 153 * <tr class="rowColor"> 154 * <td><p>onkeyup</p></td> 155 * <td><p>false</p></td> 156 * <td><p>true</p></td> 157 * <td><p>HTML Event Attribute</p></td> 158 * </tr> 159 * <tr class="altColor"> 160 * <td><p>onmousedown</p></td> 161 * <td><p>false</p></td> 162 * <td><p>true</p></td> 163 * <td><p>HTML Event Attribute</p></td> 164 * </tr> 165 * <tr class="rowColor"> 166 * <td><p>onmousemove</p></td> 167 * <td><p>false</p></td> 168 * <td><p>true</p></td> 169 * <td><p>HTML Event Attribute</p></td> 170 * </tr> 171 * <tr class="altColor"> 172 * <td><p>onmouseout</p></td> 173 * <td><p>false</p></td> 174 * <td><p>true</p></td> 175 * <td><p>HTML Event Attribute</p></td> 176 * </tr> 177 * <tr class="rowColor"> 178 * <td><p>onmouseover</p></td> 179 * <td><p>false</p></td> 180 * <td><p>true</p></td> 181 * <td><p>HTML Event Attribute</p></td> 182 * </tr> 183 * <tr class="altColor"> 184 * <td><p>onmouseup</p></td> 185 * <td><p>false</p></td> 186 * <td><p>true</p></td> 187 * <td><p>HTML Event Attribute</p></td> 188 * </tr> 189 * <tr class="rowColor"> 190 * <td><p>onselect</p></td> 191 * <td><p>false</p></td> 192 * <td><p>true</p></td> 193 * <td><p>HTML Event Attribute</p></td> 194 * </tr> 195 * <tr class="altColor"> 196 * <td><p>path</p></td> 197 * <td><p>true</p></td> 198 * <td><p>true</p></td> 199 * <td><p>Path to property for data binding</p></td> 200 * </tr> 201 * <tr class="rowColor"> 202 * <td><p>readonly</p></td> 203 * <td><p>false</p></td> 204 * <td><p>true</p></td> 205 * <td><p>HTML Optional Attribute. Setting the value of this attribute to 'true' 206 * will make the HTML element readonly.</p></td> 207 * </tr> 208 * <tr class="altColor"> 209 * <td><p>showPassword</p></td> 210 * <td><p>false</p></td> 211 * <td><p>true</p></td> 212 * <td><p>Is the password value to be shown? Defaults to false.</p></td> 213 * </tr> 214 * <tr class="rowColor"> 215 * <td><p>size</p></td> 216 * <td><p>false</p></td> 217 * <td><p>true</p></td> 218 * <td><p>HTML Optional Attribute</p></td> 219 * </tr> 220 * <tr class="altColor"> 221 * <td><p>tabindex</p></td> 222 * <td><p>false</p></td> 223 * <td><p>true</p></td> 224 * <td><p>HTML Standard Attribute</p></td> 225 * </tr> 226 * <tr class="rowColor"> 227 * <td><p>title</p></td> 228 * <td><p>false</p></td> 229 * <td><p>true</p></td> 230 * <td><p>HTML Standard Attribute</p></td> 231 * </tr> 232 * </tbody> 233 * </table> 234 * 235 * @author Rob Harrop 236 * @author Rick Evans 237 * @author Rossen Stoyanchev 238 * @since 2.0 239 */ 240@SuppressWarnings("serial") 241public class PasswordInputTag extends InputTag { 242 243 private boolean showPassword = false; 244 245 246 /** 247 * Is the password value to be rendered? 248 * @param showPassword {@code true} if the password value is to be rendered 249 */ 250 public void setShowPassword(boolean showPassword) { 251 this.showPassword = showPassword; 252 } 253 254 /** 255 * Is the password value to be rendered? 256 * @return {@code true} if the password value to be rendered 257 */ 258 public boolean isShowPassword() { 259 return this.showPassword; 260 } 261 262 263 /** 264 * Flags "type" as an illegal dynamic attribute. 265 */ 266 @Override 267 protected boolean isValidDynamicAttribute(String localName, Object value) { 268 return !"type".equals(localName); 269 } 270 271 /** 272 * Return '{@code password}' causing the rendered HTML '{@code input}' 273 * element to have a '{@code type}' of '{@code password}'. 274 */ 275 @Override 276 protected String getType() { 277 return "password"; 278 } 279 280 /** 281 * The {@link PasswordInputTag} only writes it's value if the 282 * {@link #setShowPassword(boolean) 'showPassword'} property value is 283 * {@link Boolean#TRUE true}. 284 */ 285 @Override 286 protected void writeValue(TagWriter tagWriter) throws JspException { 287 if (this.showPassword) { 288 super.writeValue(tagWriter); 289 } 290 else { 291 tagWriter.writeAttribute("value", processFieldValue(getName(), "", getType())); 292 } 293 } 294 295}