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;
018
019import javax.servlet.jsp.JspException;
020import javax.servlet.jsp.tagext.BodyTagSupport;
021
022import org.springframework.lang.Nullable;
023
024/**
025 * The {@code <param>} tag collects name-value parameters and passes them to a
026 * {@link ParamAware} ancestor in the tag hierarchy.
027 *
028 * <p>This tag must be nested under a param aware tag.
029 *
030 * <table>
031 * <caption>Attribute Summary</caption>
032 * <thead>
033 * <tr>
034 * <th>Attribute</th>
035 * <th>Required?</th>
036 * <th>Runtime Expression?</th>
037 * <th>Description</th>
038 * </tr>
039 * </thead>
040 * <tbody>
041 * <tr>
042 * <td>name</td>
043 * <td>true</td>
044 * <td>true</td>
045 * <td>The name of the parameter.</td>
046 * </tr>
047 * <tr>
048 * <td>value</td>
049 * <td>false</td>
050 * <td>true</td>
051 * <td>The value of the parameter.</td>
052 * </tr>
053 * </tbody>
054 * </table>
055 *
056 * @author Scott Andrews
057 * @author Nicholas Williams
058 * @since 3.0
059 * @see Param
060 * @see UrlTag
061 */
062@SuppressWarnings("serial")
063public class ParamTag extends BodyTagSupport {
064
065        private String name = "";
066
067        @Nullable
068        private String value;
069
070        private boolean valueSet;
071
072
073        /**
074         * Set the name of the parameter (required).
075         */
076        public void setName(String name) {
077                this.name = name;
078        }
079
080        /**
081         * Set the value of the parameter (optional).
082         */
083        public void setValue(String value) {
084                this.value = value;
085                this.valueSet = true;
086        }
087
088
089        @Override
090        public int doEndTag() throws JspException {
091                Param param = new Param();
092                param.setName(this.name);
093                if (this.valueSet) {
094                        param.setValue(this.value);
095                }
096                else if (getBodyContent() != null) {
097                        // Get the value from the tag body
098                        param.setValue(getBodyContent().getString().trim());
099                }
100
101                // Find a param aware ancestor
102                ParamAware paramAwareTag = (ParamAware) findAncestorWithClass(this, ParamAware.class);
103                if (paramAwareTag == null) {
104                        throw new JspException("The param tag must be a descendant of a tag that supports parameters");
105                }
106
107                paramAwareTag.addParam(param);
108
109                return EVAL_PAGE;
110        }
111
112        @Override
113        public void release() {
114                super.release();
115                this.name = "";
116                this.value = null;
117                this.valueSet = false;
118        }
119
120}