001/*
002 * Copyright 2002-2012 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 * Abstract base class to provide common methods for implementing
023 * databinding-aware JSP tags for rendering a <i>single</i>
024 * HTML '{@code input}' element with a '{@code type}'
025 * of '{@code checkbox}' or '{@code radio}'.
026 *
027 * @author Juergen Hoeller
028 * @since 2.5.2
029 */
030@SuppressWarnings("serial")
031public abstract class AbstractSingleCheckedElementTag extends AbstractCheckedElementTag {
032
033        /**
034         * The value of the '{@code value}' attribute.
035         */
036        private Object value;
037
038        /**
039         * The value of the '{@code label}' attribute.
040         */
041        private Object label;
042
043
044        /**
045         * Set the value of the '{@code value}' attribute.
046         * May be a runtime expression.
047         */
048        public void setValue(Object value) {
049                this.value = value;
050        }
051
052        /**
053         * Get the value of the '{@code value}' attribute.
054         */
055        protected Object getValue() {
056                return this.value;
057        }
058
059        /**
060         * Set the value of the '{@code label}' attribute.
061         * May be a runtime expression.
062         */
063        public void setLabel(Object label) {
064                this.label = label;
065        }
066
067        /**
068         * Get the value of the '{@code label}' attribute.
069         */
070        protected Object getLabel() {
071                return this.label;
072        }
073
074
075        /**
076         * Renders the '{@code input(radio)}' element with the configured
077         * {@link #setValue(Object) value}. Marks the element as checked if the
078         * value matches the {@link #getValue bound value}.
079         */
080        @Override
081        protected int writeTagContent(TagWriter tagWriter) throws JspException {
082                tagWriter.startTag("input");
083                String id = resolveId();
084                writeOptionalAttribute(tagWriter, "id", id);
085                writeOptionalAttribute(tagWriter, "name", getName());
086                writeOptionalAttributes(tagWriter);
087                writeTagDetails(tagWriter);
088                tagWriter.endTag();
089
090                Object resolvedLabel = evaluate("label", getLabel());
091                if (resolvedLabel != null) {
092                        tagWriter.startTag("label");
093                        tagWriter.writeAttribute("for", id);
094                        tagWriter.appendValue(convertToDisplayString(resolvedLabel));
095                        tagWriter.endTag();
096                }
097
098                return SKIP_BODY;
099        }
100
101        /**
102         * Write the details for the given primary tag:
103         * i.e. special attributes and the tag's value.
104         */
105        protected abstract void writeTagDetails(TagWriter tagWriter) throws JspException;
106
107}