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 <argument>} tag is based on the JSTL {@code fmt:param} tag. 026 * The purpose is to support arguments inside the message and theme tags. 027 * 028 * <p>This tag must be nested under an argument 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>value</td> 043 * <td>false</td> 044 * <td>true</td> 045 * <td>The value of the argument.</td> 046 * </tr> 047 * </tbody> 048 * </table> 049 * 050 * @author Nicholas Williams 051 * @since 4.0 052 * @see MessageTag 053 * @see ThemeTag 054 */ 055@SuppressWarnings("serial") 056public class ArgumentTag extends BodyTagSupport { 057 058 @Nullable 059 private Object value; 060 061 private boolean valueSet; 062 063 064 /** 065 * Set the value of the argument (optional). 066 * If not set, the tag's body content will get evaluated. 067 * @param value the parameter value 068 */ 069 public void setValue(Object value) { 070 this.value = value; 071 this.valueSet = true; 072 } 073 074 075 @Override 076 public int doEndTag() throws JspException { 077 Object argument = null; 078 if (this.valueSet) { 079 argument = this.value; 080 } 081 else if (getBodyContent() != null) { 082 // Get the value from the tag body 083 argument = getBodyContent().getString().trim(); 084 } 085 086 // Find a param-aware ancestor 087 ArgumentAware argumentAwareTag = (ArgumentAware) findAncestorWithClass(this, ArgumentAware.class); 088 if (argumentAwareTag == null) { 089 throw new JspException("The argument tag must be a descendant of a tag that supports arguments"); 090 } 091 argumentAwareTag.addArgument(argument); 092 093 return EVAL_PAGE; 094 } 095 096 @Override 097 public void release() { 098 super.release(); 099 this.value = null; 100 this.valueSet = false; 101 } 102 103}