KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > tags > form > LabelTag


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.tags.form;
18
19 import javax.servlet.jsp.JspException JavaDoc;
20
21 import org.springframework.util.Assert;
22 import org.springframework.util.StringUtils;
23
24 /**
25  * Databinding-aware JSP tag for rendering an HTML '<code>label</code>' element
26  * that defines text that is associated with a single form element.
27  *
28  * <p>The {@link #setFor(String) 'for'} attribute is required.
29  *
30  * <p>See the "formTags" showcase application that ships with the
31  * full Spring distribution for an example of this class in action.
32  *
33  * @author Rob Harrop
34  * @author Juergen Hoeller
35  * @since 2.0
36  */

37 public class LabelTag extends AbstractHtmlElementTag {
38
39     /**
40      * The HTML '<code>label</code>' tag.
41      */

42     private static final String JavaDoc LABEL_TAG = "label";
43
44     /**
45      * The name of the '<code>for</code>' attribute.
46      */

47     private static final String JavaDoc FOR_ATTRIBUTE = "for";
48
49
50     /**
51      * The {@link TagWriter} instance being used.
52      * <p>Stored so we can close the tag on {@link #doEndTag()}.
53      */

54     private TagWriter tagWriter;
55
56     /**
57      * The value of the '<code>for</code>' attribute.
58      */

59     private String JavaDoc forId;
60
61
62     /**
63      * Set the value of the '<code>for</code>' attribute.
64      * <p>Defaults to the value of {@link #getPath}; may be a runtime expression.
65      * @throws IllegalArgumentException if the supplied value is <code>null</code>
66      */

67     public void setFor(String JavaDoc forId) {
68         Assert.notNull(forId, "'forId' must not be null");
69         this.forId = forId;
70     }
71
72     /**
73      * Get the value of the '<code>id</code>' attribute.
74      * <p>May be a runtime expression.
75      */

76     public String JavaDoc getFor() {
77         return this.forId;
78     }
79
80
81     /**
82      * Writes the opening '<code>label</code>' tag and forces a block tag so
83      * that body content is written correctly.
84      * @return {@link javax.servlet.jsp.tagext.Tag#EVAL_BODY_INCLUDE}
85      */

86     protected int writeTagContent(TagWriter tagWriter) throws JspException JavaDoc {
87         tagWriter.startTag(LABEL_TAG);
88         tagWriter.writeAttribute(FOR_ATTRIBUTE, resolveFor());
89         writeDefaultAttributes(tagWriter);
90         tagWriter.forceBlock();
91         this.tagWriter = tagWriter;
92         return EVAL_BODY_INCLUDE;
93     }
94
95     /**
96      * Overrides {@link #getName()} to always return the empty string "",
97      * because the '<code>name</code>' attribute is not supported by the
98      * '<code>label</code>' tag.
99      * @return the value for the HTML '<code>name</code>' attribute
100      */

101     protected String JavaDoc getName() throws JspException JavaDoc {
102         // This also suppresses the 'id' attribute (which is okay for a <label/>)
103
return "";
104     }
105
106     /**
107      * Returns the value that must be used for the '<code>for</code>' attribute.
108      */

109     protected final String JavaDoc resolveFor() throws JspException JavaDoc {
110         if (StringUtils.hasText(this.forId)) {
111             return getDisplayString(evaluate(FOR_ATTRIBUTE, this.forId));
112         }
113         else {
114             return getCompletePath();
115         }
116     }
117
118     /**
119      * Close the '<code>label</code>' tag.
120      * @return {@link javax.servlet.jsp.tagext.Tag#EVAL_PAGE}
121      */

122     public int doEndTag() throws JspException JavaDoc {
123         this.tagWriter.endTag();
124         return EVAL_PAGE;
125     }
126
127     /**
128      * Disposes of the {@link TagWriter} instance.
129      */

130     public void doFinally() {
131         super.doFinally();
132         this.tagWriter = null;
133     }
134
135 }
136
Popular Tags