KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
22  * Databinding-aware JSP tag for rendering an HTML '<code>input</code>'
23  * element with a '<code>type</code>' of '<code>radio</code>'.
24  *
25  * <p>Rendered elements are marked as 'checked' if the configured
26  * {@link #setValue(Object) value} matches the {@link #getValue bound value}.
27  *
28  * <p>A typical usage pattern will involved multiple tag instances bound
29  * to the same property but with different values.
30  *
31  * @author Rob Harrop
32  * @since 2.0
33  */

34 public class RadioButtonTag extends AbstractHtmlInputElementTag {
35
36     /**
37      * The value of the '<code>value</code>' attribute.
38      */

39     private Object JavaDoc value;
40
41
42     /**
43      * Set the value of the '<code>value</code>' attribute.
44      * May be a runtime expression.
45      */

46     public void setValue(Object JavaDoc value) {
47         this.value = value;
48     }
49
50     /**
51      * Get the value of the '<code>value</code>' attribute.
52      * May be a runtime expression.
53      */

54     protected Object JavaDoc getValue() {
55         return this.value;
56     }
57
58
59     /**
60      * Renders the '<code>input(radio)</code>' element with the configured
61      * {@link #setValue(Object) value}. Marks the element as checked if the
62      * value matches the {@link #getValue bound value}.
63      */

64     protected int writeTagContent(TagWriter tagWriter) throws JspException JavaDoc {
65         tagWriter.startTag("input");
66         writeDefaultAttributes(tagWriter);
67         tagWriter.writeAttribute("type", "radio");
68
69         Object JavaDoc value = getValue();
70         Object JavaDoc resolvedValue = evaluate("value", value);
71         tagWriter.writeAttribute("value", getDisplayString(resolvedValue, getPropertyEditor()));
72
73         if (SelectedValueComparator.isSelected(getBindStatus(), resolvedValue)) {
74             tagWriter.writeAttribute("checked", "checked");
75         }
76
77         tagWriter.endTag();
78         return EVAL_PAGE;
79     }
80
81     /**
82      * Return a unique ID for the bound name within the current PageContext.
83      */

84     protected String JavaDoc autogenerateId() throws JspException JavaDoc {
85         return TagIdGenerator.nextId(getName(), this.pageContext);
86     }
87
88 }
89
Popular Tags