001/* 002 * Copyright 2002-2015 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 022/** 023 * JSP tag for collecting arguments and passing them to an {@link ArgumentAware} 024 * ancestor in the tag hierarchy. 025 * 026 * <p>This tag must be nested under an argument aware tag. 027 * 028 * @author Nicholas Williams 029 * @since 4.0 030 * @see MessageTag 031 * @see ThemeTag 032 */ 033@SuppressWarnings("serial") 034public class ArgumentTag extends BodyTagSupport { 035 036 private Object value; 037 038 private boolean valueSet; 039 040 041 /** 042 * Set the value of the argument (optional). 043 * <pIf not set, the tag's body content will get evaluated. 044 * @param value the parameter value 045 */ 046 public void setValue(Object value) { 047 this.value = value; 048 this.valueSet = true; 049 } 050 051 052 @Override 053 public int doEndTag() throws JspException { 054 Object argument = null; 055 if (this.valueSet) { 056 argument = this.value; 057 } 058 else if (getBodyContent() != null) { 059 // Get the value from the tag body 060 argument = getBodyContent().getString().trim(); 061 } 062 063 // Find a param-aware ancestor 064 ArgumentAware argumentAwareTag = (ArgumentAware) findAncestorWithClass(this, ArgumentAware.class); 065 if (argumentAwareTag == null) { 066 throw new JspException("The argument tag must be a descendant of a tag that supports arguments"); 067 } 068 argumentAwareTag.addArgument(argument); 069 070 return EVAL_PAGE; 071 } 072 073 @Override 074 public void release() { 075 super.release(); 076 this.value = null; 077 this.valueSet = false; 078 } 079 080}