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 java.beans.PropertyEditor; 020import java.io.IOException; 021import javax.servlet.jsp.JspException; 022import javax.servlet.jsp.tagext.TagSupport; 023 024import org.springframework.web.util.TagUtils; 025 026/** 027 * Tag for transforming reference data values from form controllers and 028 * other objects inside a {@code spring:bind} tag (or a data-bound 029 * form element tag from Spring's form tag library). 030 * 031 * <p>The BindTag has a PropertyEditor that it uses to transform properties of 032 * a bean to a String, usable in HTML forms. This tag uses that PropertyEditor 033 * to transform objects passed into this tag. 034 * 035 * @author Alef Arendsen 036 * @author Juergen Hoeller 037 * @since 20.09.2003 038 * @see BindTag 039 */ 040@SuppressWarnings("serial") 041public class TransformTag extends HtmlEscapingAwareTag { 042 043 /** the value to transform using the appropriate property editor */ 044 private Object value; 045 046 /** the variable to put the result in */ 047 private String var; 048 049 /** the scope of the variable the result will be put in */ 050 private String scope = TagUtils.SCOPE_PAGE; 051 052 053 /** 054 * Set the value to transform, using the appropriate PropertyEditor 055 * from the enclosing BindTag. 056 * <p>The value can either be a plain value to transform (a hard-coded String 057 * value in a JSP or a JSP expression), or a JSP EL expression to be evaluated 058 * (transforming the result of the expression). 059 */ 060 public void setValue(Object value) { 061 this.value = value; 062 } 063 064 /** 065 * Set PageContext attribute name under which to expose 066 * a variable that contains the result of the transformation. 067 * @see #setScope 068 * @see javax.servlet.jsp.PageContext#setAttribute 069 */ 070 public void setVar(String var) { 071 this.var = var; 072 } 073 074 /** 075 * Set the scope to export the variable to. 076 * Default is SCOPE_PAGE ("page"). 077 * @see #setVar 078 * @see org.springframework.web.util.TagUtils#SCOPE_PAGE 079 * @see javax.servlet.jsp.PageContext#setAttribute 080 */ 081 public void setScope(String scope) { 082 this.scope = scope; 083 } 084 085 086 @Override 087 protected final int doStartTagInternal() throws JspException { 088 if (this.value != null) { 089 // Find the containing EditorAwareTag (e.g. BindTag), if applicable. 090 EditorAwareTag tag = (EditorAwareTag) TagSupport.findAncestorWithClass(this, EditorAwareTag.class); 091 if (tag == null) { 092 throw new JspException("TransformTag can only be used within EditorAwareTag (e.g. BindTag)"); 093 } 094 095 // OK, let's obtain the editor... 096 String result = null; 097 PropertyEditor editor = tag.getEditor(); 098 if (editor != null) { 099 // If an editor was found, edit the value. 100 editor.setValue(this.value); 101 result = editor.getAsText(); 102 } 103 else { 104 // Else, just do a toString. 105 result = this.value.toString(); 106 } 107 result = htmlEscape(result); 108 if (this.var != null) { 109 pageContext.setAttribute(this.var, result, TagUtils.getScope(this.scope)); 110 } 111 else { 112 try { 113 // Else, just print it out. 114 pageContext.getOut().print(result); 115 } 116 catch (IOException ex) { 117 throw new JspException(ex); 118 } 119 } 120 } 121 122 return SKIP_BODY; 123 } 124 125}