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.web.servlet.support.RequestDataValueProcessor; 022 023/** 024 * An HTML button tag. This tag is provided for completeness if the application 025 * relies on a {@link RequestDataValueProcessor}. 026 * 027 * @author Rossen Stoyanchev 028 * @since 3.1 029 */ 030@SuppressWarnings("serial") 031public class ButtonTag extends AbstractHtmlElementTag { 032 033 /** 034 * The name of the '{@code disabled}' attribute. 035 */ 036 public static final String DISABLED_ATTRIBUTE = "disabled"; 037 038 039 private TagWriter tagWriter; 040 041 private String name; 042 043 private String value; 044 045 private boolean disabled; 046 047 048 /** 049 * Get the value of the '{@code name}' attribute. 050 */ 051 public void setName(String name) { 052 this.name = name; 053 } 054 055 /** 056 * Set the value of the '{@code name}' attribute. 057 */ 058 @Override 059 public String getName() { 060 return this.name; 061 } 062 063 /** 064 * Set the value of the '{@code value}' attribute. 065 */ 066 public void setValue(String value) { 067 this.value = value; 068 } 069 070 /** 071 * Get the value of the '{@code value}' attribute. 072 */ 073 public String getValue() { 074 return this.value; 075 } 076 077 /** 078 * Set the value of the '{@code disabled}' attribute. 079 */ 080 public void setDisabled(boolean disabled) { 081 this.disabled = disabled; 082 } 083 084 /** 085 * Get the value of the '{@code disabled}' attribute. 086 */ 087 public boolean isDisabled() { 088 return this.disabled; 089 } 090 091 092 @Override 093 protected int writeTagContent(TagWriter tagWriter) throws JspException { 094 tagWriter.startTag("button"); 095 writeDefaultAttributes(tagWriter); 096 tagWriter.writeAttribute("type", getType()); 097 writeValue(tagWriter); 098 if (isDisabled()) { 099 tagWriter.writeAttribute(DISABLED_ATTRIBUTE, "disabled"); 100 } 101 tagWriter.forceBlock(); 102 this.tagWriter = tagWriter; 103 return EVAL_BODY_INCLUDE; 104 } 105 106 /** 107 * Writes the '{@code value}' attribute to the supplied {@link TagWriter}. 108 * Subclasses may choose to override this implementation to control exactly 109 * when the value is written. 110 */ 111 protected void writeValue(TagWriter tagWriter) throws JspException { 112 String valueToUse = (getValue() != null ? getValue() : getDefaultValue()); 113 tagWriter.writeAttribute("value", processFieldValue(getName(), valueToUse, getType())); 114 } 115 116 /** 117 * Return the default value. 118 * @return the default value if none supplied 119 */ 120 protected String getDefaultValue() { 121 return "Submit"; 122 } 123 124 /** 125 * Get the value of the '{@code type}' attribute. Subclasses 126 * can override this to change the type of '{@code input}' element 127 * rendered. Default value is '{@code submit}'. 128 */ 129 protected String getType() { 130 return "submit"; 131 } 132 133 /** 134 * Closes the '{@code button}' block tag. 135 */ 136 @Override 137 public int doEndTag() throws JspException { 138 this.tagWriter.endTag(); 139 return EVAL_PAGE; 140 } 141 142}