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 * Databinding-aware JSP tag for rendering an HTML '{@code input}'
023 * element with a '{@code type}' of '{@code password}'.
024 *
025 * @author Rob Harrop
026 * @author Rick Evans
027 * @author Rossen Stoyanchev
028 * @since 2.0
029 */
030@SuppressWarnings("serial")
031public class PasswordInputTag extends InputTag {
032
033        private boolean showPassword = false;
034
035
036        /**
037         * Is the password value to be rendered?
038         * @param showPassword {@code true} if the password value is to be rendered
039         */
040        public void setShowPassword(boolean showPassword) {
041                this.showPassword = showPassword;
042        }
043
044        /**
045         * Is the password value to be rendered?
046         * @return {@code true} if the password value to be rendered
047         */
048        public boolean isShowPassword() {
049                return this.showPassword;
050        }
051
052
053        /**
054         * Flags "type" as an illegal dynamic attribute.
055         */
056        @Override
057        protected boolean isValidDynamicAttribute(String localName, Object value) {
058                return !"type".equals(localName);
059        }
060
061        /**
062         * Return '{@code password}' causing the rendered HTML '{@code input}'
063         * element to have a '{@code type}' of '{@code password}'.
064         */
065        @Override
066        protected String getType() {
067                return "password";
068        }
069
070        /**
071         * The {@link PasswordInputTag} only writes it's value if the
072         * {@link #setShowPassword(boolean) 'showPassword'} property value is
073         * {@link Boolean#TRUE true}.
074         */
075        @Override
076        protected void writeValue(TagWriter tagWriter) throws JspException {
077                if (this.showPassword) {
078                        super.writeValue(tagWriter);
079                }
080                else {
081                        tagWriter.writeAttribute("value", processFieldValue(getName(), "", getType()));
082                }
083        }
084
085}