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.form; 018 019import javax.servlet.jsp.JspException; 020 021import org.springframework.lang.Nullable; 022import org.springframework.util.Assert; 023import org.springframework.web.servlet.support.RequestDataValueProcessor; 024 025/** 026 * The {@code <button>} tag renders a form field label in an HTML 'button' tag. 027 * It is provided for completeness if the application relies on a 028 * {@link RequestDataValueProcessor}. 029 * 030 * <p> 031 * <table> 032 * <caption>Attribute Summary</caption> 033 * <thead> 034 * <tr> 035 * <th class="colFirst">Attribute</th> 036 * <th class="colOne">Required?</th> 037 * <th class="colOne">Runtime Expression?</th> 038 * <th class="colLast">Description</th> 039 * </tr> 040 * </thead> 041 * <tbody> 042 * <tr class="altColor"> 043 * <td><p>disabled</p></td> 044 * <td><p>false</p></td> 045 * <td><p>true</p></td> 046 * <td><p>HTML Optional Attribute. Setting the value of this attribute to 'true' 047 * will disable the HTML element.</p></td> 048 * </tr> 049 * <tr class="rowColor"> 050 * <td><p>id</p></td> 051 * <td><p>false</p></td> 052 * <td><p>true</p></td> 053 * <td><p>HTML Standard Attribute</p></td> 054 * </tr> 055 * <tr class="altColor"> 056 * <td><p>name</p></td> 057 * <td><p>false</p></td> 058 * <td><p>true</p></td> 059 * <td><p>The name attribute for the HTML button tag</p></td> 060 * </tr> 061 * <tr class="rowColor"> 062 * <td><p>value</p></td> 063 * <td><p>false</p></td> 064 * <td><p>true</p></td> 065 * <td><p>The name attribute for the HTML button tag</p></td> 066 * </tr> 067 * </tbody> 068 * </table> 069 * 070 * @author Rossen Stoyanchev 071 * @since 3.1 072 */ 073@SuppressWarnings("serial") 074public class ButtonTag extends AbstractHtmlElementTag { 075 076 /** 077 * The name of the '{@code disabled}' attribute. 078 */ 079 public static final String DISABLED_ATTRIBUTE = "disabled"; 080 081 082 @Nullable 083 private TagWriter tagWriter; 084 085 @Nullable 086 private String name; 087 088 @Nullable 089 private String value; 090 091 private boolean disabled; 092 093 094 /** 095 * Get the value of the '{@code name}' attribute. 096 */ 097 public void setName(String name) { 098 this.name = name; 099 } 100 101 /** 102 * Set the value of the '{@code name}' attribute. 103 */ 104 @Override 105 @Nullable 106 public String getName() { 107 return this.name; 108 } 109 110 /** 111 * Set the value of the '{@code value}' attribute. 112 */ 113 public void setValue(@Nullable String value) { 114 this.value = value; 115 } 116 117 /** 118 * Get the value of the '{@code value}' attribute. 119 */ 120 @Nullable 121 public String getValue() { 122 return this.value; 123 } 124 125 /** 126 * Set the value of the '{@code disabled}' attribute. 127 */ 128 public void setDisabled(boolean disabled) { 129 this.disabled = disabled; 130 } 131 132 /** 133 * Get the value of the '{@code disabled}' attribute. 134 */ 135 public boolean isDisabled() { 136 return this.disabled; 137 } 138 139 140 @Override 141 protected int writeTagContent(TagWriter tagWriter) throws JspException { 142 tagWriter.startTag("button"); 143 writeDefaultAttributes(tagWriter); 144 tagWriter.writeAttribute("type", getType()); 145 writeValue(tagWriter); 146 if (isDisabled()) { 147 tagWriter.writeAttribute(DISABLED_ATTRIBUTE, "disabled"); 148 } 149 tagWriter.forceBlock(); 150 this.tagWriter = tagWriter; 151 return EVAL_BODY_INCLUDE; 152 } 153 154 /** 155 * Writes the '{@code value}' attribute to the supplied {@link TagWriter}. 156 * Subclasses may choose to override this implementation to control exactly 157 * when the value is written. 158 */ 159 protected void writeValue(TagWriter tagWriter) throws JspException { 160 String valueToUse = (getValue() != null ? getValue() : getDefaultValue()); 161 tagWriter.writeAttribute("value", processFieldValue(getName(), valueToUse, getType())); 162 } 163 164 /** 165 * Return the default value. 166 * @return the default value if none supplied 167 */ 168 protected String getDefaultValue() { 169 return "Submit"; 170 } 171 172 /** 173 * Get the value of the '{@code type}' attribute. Subclasses 174 * can override this to change the type of '{@code input}' element 175 * rendered. Default value is '{@code submit}'. 176 */ 177 protected String getType() { 178 return "submit"; 179 } 180 181 /** 182 * Closes the '{@code button}' block tag. 183 */ 184 @Override 185 public int doEndTag() throws JspException { 186 Assert.state(this.tagWriter != null, "No TagWriter set"); 187 this.tagWriter.endTag(); 188 return EVAL_PAGE; 189 } 190 191}