/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.tags.form;
import javax.servlet.jsp.JspException;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
The <label>
tag renders a form field label in an HTML 'label' tag. See the "formTags" showcase application that ships with the
full Spring distribution for an example of this class in action.
Attribute Summary
Attribute
Required?
Runtime Expression?
Description
cssClass
false
true
HTML Optional Attribute.
cssErrorClass
false
true
HTML Optional Attribute. Used only when errors are present.
cssStyle
false
true
HTML Optional Attribute
dir
false
true
HTML Standard Attribute
for
false
true
HTML Standard Attribute
htmlEscape
false
true
Enable/disable HTML escaping of rendered values.
id
false
true
HTML Standard Attribute
lang
false
true
HTML Standard Attribute
onclick
false
true
HTML Event Attribute
ondblclick
false
true
HTML Event Attribute
onkeydown
false
true
HTML Event Attribute
onkeypress
false
true
HTML Event Attribute
onkeyup
false
true
HTML Event Attribute
onmousedown
false
true
HTML Event Attribute
onmousemove
false
true
HTML Event Attribute
onmouseout
false
true
HTML Event Attribute
onmouseover
false
true
HTML Event Attribute
onmouseup
false
true
HTML Event Attribute
path
true
true
Path to errors object for data binding
tabindex
false
true
HTML Standard Attribute
title
false
true
HTML Standard Attribute
Author: Rob Harrop, Juergen Hoeller Since: 2.0
/**
* The {@code <label>} tag renders a form field label in an HTML 'label' tag.
*
* <p>See the "formTags" showcase application that ships with the
* full Spring distribution for an example of this class in action.
*
* <p>
* <table>
* <caption>Attribute Summary</caption>
* <thead>
* <tr>
* <th class="colFirst">Attribute</th>
* <th class="colOne">Required?</th>
* <th class="colOne">Runtime Expression?</th>
* <th class="colLast">Description</th>
* </tr>
* </thead>
* <tbody>
* <tr class="altColor">
* <td><p>cssClass</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Optional Attribute.</p></td>
* </tr>
* <tr class="rowColor">
* <td><p>cssErrorClass</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Optional Attribute. Used only when errors are present.</p></td>
* </tr>
* <tr class="altColor">
* <td><p>cssStyle</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Optional Attribute</p></td>
* </tr>
* <tr class="rowColor">
* <td><p>dir</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Standard Attribute</p></td>
* </tr>
* <tr class="altColor">
* <td><p>for</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Standard Attribute</p></td>
* </tr>
* <tr class="rowColor">
* <td><p>htmlEscape</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>Enable/disable HTML escaping of rendered values.</p></td>
* </tr>
* <tr class="altColor">
* <td><p>id</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Standard Attribute</p></td>
* </tr>
* <tr class="rowColor">
* <td><p>lang</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Standard Attribute</p></td>
* </tr>
* <tr class="altColor">
* <td><p>onclick</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Event Attribute</p></td>
* </tr>
* <tr class="rowColor">
* <td><p>ondblclick</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Event Attribute</p></td>
* </tr>
* <tr class="altColor">
* <td><p>onkeydown</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Event Attribute</p></td>
* </tr>
* <tr class="rowColor">
* <td><p>onkeypress</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Event Attribute</p></td>
* </tr>
* <tr class="altColor">
* <td><p>onkeyup</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Event Attribute</p></td>
* </tr>
* <tr class="rowColor">
* <td><p>onmousedown</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Event Attribute</p></td>
* </tr>
* <tr class="altColor">
* <td><p>onmousemove</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Event Attribute</p></td>
* </tr>
* <tr class="rowColor">
* <td><p>onmouseout</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Event Attribute</p></td>
* </tr>
* <tr class="altColor">
* <td><p>onmouseover</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Event Attribute</p></td>
* </tr>
* <tr class="rowColor">
* <td><p>onmouseup</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Event Attribute</p></td>
* </tr>
* <tr class="altColor">
* <td><p>path</p></td>
* <td><p>true</p></td>
* <td><p>true</p></td>
* <td><p>Path to errors object for data binding</p></td>
* </tr>
* <tr class="rowColor">
* <td><p>tabindex</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Standard Attribute</p></td>
* </tr>
* <tr class="altColor">
* <td><p>title</p></td>
* <td><p>false</p></td>
* <td><p>true</p></td>
* <td><p>HTML Standard Attribute</p></td>
* </tr>
* </tbody>
* </table>
*
* @author Rob Harrop
* @author Juergen Hoeller
* @since 2.0
*/
@SuppressWarnings("serial")
public class LabelTag extends AbstractHtmlElementTag {
The HTML 'label
' tag. /**
* The HTML '{@code label}' tag.
*/
private static final String LABEL_TAG = "label";
The name of the 'for
' attribute. /**
* The name of the '{@code for}' attribute.
*/
private static final String FOR_ATTRIBUTE = "for";
The TagWriter
instance being used. Stored so we can close the tag on doEndTag()
.
/**
* The {@link TagWriter} instance being used.
* <p>Stored so we can close the tag on {@link #doEndTag()}.
*/
@Nullable
private TagWriter tagWriter;
The value of the 'for
' attribute. /**
* The value of the '{@code for}' attribute.
*/
@Nullable
private String forId;
Set the value of the 'for
' attribute. Defaults to the value of getPath
; may be a runtime expression.
/**
* Set the value of the '{@code for}' attribute.
* <p>Defaults to the value of {@link #getPath}; may be a runtime expression.
*/
public void setFor(String forId) {
this.forId = forId;
}
Get the value of the 'id
' attribute. May be a runtime expression.
/**
* Get the value of the '{@code id}' attribute.
* <p>May be a runtime expression.
*/
@Nullable
protected String getFor() {
return this.forId;
}
Writes the opening 'label
' tag and forces a block tag so that body content is written correctly. Returns: EVAL_BODY_INCLUDE.EVAL_BODY_INCLUDE
/**
* Writes the opening '{@code label}' tag and forces a block tag so
* that body content is written correctly.
* @return {@link javax.servlet.jsp.tagext.Tag#EVAL_BODY_INCLUDE}
*/
@Override
protected int writeTagContent(TagWriter tagWriter) throws JspException {
tagWriter.startTag(LABEL_TAG);
tagWriter.writeAttribute(FOR_ATTRIBUTE, resolveFor());
writeDefaultAttributes(tagWriter);
tagWriter.forceBlock();
this.tagWriter = tagWriter;
return EVAL_BODY_INCLUDE;
}
Overrides #getName()
to always return null
, because the 'name
' attribute is not supported by the 'label
' tag. Returns: the value for the HTML 'name
' attribute
/**
* Overrides {@code #getName()} to always return {@code null},
* because the '{@code name}' attribute is not supported by the
* '{@code label}' tag.
* @return the value for the HTML '{@code name}' attribute
*/
@Override
@Nullable
protected String getName() throws JspException {
// This also suppresses the 'id' attribute (which is okay for a <label/>)
return null;
}
Determine the 'for
' attribute value for this tag, autogenerating one if none specified. See Also: - getFor()
- autogenerateFor()
/**
* Determine the '{@code for}' attribute value for this tag,
* autogenerating one if none specified.
* @see #getFor()
* @see #autogenerateFor()
*/
protected String resolveFor() throws JspException {
if (StringUtils.hasText(this.forId)) {
return getDisplayString(evaluate(FOR_ATTRIBUTE, this.forId));
}
else {
return autogenerateFor();
}
}
Autogenerate the 'for
' attribute value for this tag. The default implementation delegates to getPropertyPath()
, deleting invalid characters (such as "[" or "]").
/**
* Autogenerate the '{@code for}' attribute value for this tag.
* <p>The default implementation delegates to {@link #getPropertyPath()},
* deleting invalid characters (such as "[" or "]").
*/
protected String autogenerateFor() throws JspException {
return StringUtils.deleteAny(getPropertyPath(), "[]");
}
Close the 'label
' tag. /**
* Close the '{@code label}' tag.
*/
@Override
public int doEndTag() throws JspException {
Assert.state(this.tagWriter != null, "No TagWriter set");
this.tagWriter.endTag();
return EVAL_PAGE;
}
Disposes of the TagWriter
instance. /**
* Disposes of the {@link TagWriter} instance.
*/
@Override
public void doFinally() {
super.doFinally();
this.tagWriter = null;
}
}