2
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/MultiboxTag.java,v 1.25 2004/03/14 06:23:46 sraeburn Exp $
4
* $Date: 2004/03/14 06:23:46 $
6
* Copyright 2001-2004 The Apache Software Foundation.
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
21
package org.apache.struts.taglib.html;
23
import java.lang.reflect.InvocationTargetException;
25
import javax.servlet.jsp.JspException;
26
import javax.servlet.jsp.PageContext;
28
import org.apache.commons.beanutils.BeanUtils;
29
import org.apache.struts.Globals;
30
import org.apache.struts.taglib.TagUtils;
31
import org.apache.struts.util.MessageResources;
34
* Tag for input fields of type "checkbox". This differs from CheckboxTag
35
* because it assumes that the underlying property is an array getter (of any
36
* supported primitive type, or String), and the checkbox is initialized to
37
* "checked" if the value listed for the "value" attribute is present in the
38
* values returned by the property getter.
40
* @version $Revision: 1.25 $ $Date: 2004/03/14 06:23:46 $
43
public class MultiboxTag extends BaseHandlerTag {
45
// ----------------------------------------------------- Instance Variables
48
* The constant String value to be returned when this checkbox is
49
* selected and the form is submitted.
51
protected String constant = null;
54
* The message resources for this package.
56
protected static MessageResources messages =
57
MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
60
* The name of the bean containing our underlying property.
62
protected String name = Constants.BEAN_KEY;
64
public String getName() {
68
public void setName(String name) {
73
* The property name for this field.
75
protected String property = null;
78
* The value which will mark this checkbox as "checked" if present
79
* in the array returned by our property getter.
81
protected String value = null;
83
// ------------------------------------------------------------- Properties
86
* Return the property name.
88
public String getProperty() {
90
return (this.property);
95
* Set the property name.
97
* @param property The new property name
99
public void setProperty(String property) {
101
this.property = property;
106
* Return the server value.
108
public String getValue() {
115
* Set the server value.
117
* @param value The new server value
119
public void setValue(String value) {
125
// --------------------------------------------------------- Public Methods
128
* Process the beginning of this tag.
130
* @exception JspException if a JSP exception has occurred
132
public int doStartTag() throws JspException {
134
// Defer processing until the end of this tag is encountered
135
this.constant = null;
136
return (EVAL_BODY_TAG);
141
* Save the body contents of this tag as the constant that we will
144
* @exception JspException if a JSP exception has occurred
146
public int doAfterBody() throws JspException {
148
if (bodyContent != null) {
149
this.constant = bodyContent.getString().trim();
152
if ("".equals(this.constant)) {
153
this.constant = null;
160
* Render an input element for this tag.
162
* @exception JspException if a JSP exception has occurred
164
public int doEndTag() throws JspException {
166
// Create an appropriate "input" element based on our parameters
167
StringBuffer results = new StringBuffer("<input type=\"checkbox\"");
168
results.append(" name=\"");
169
results.append(this.property);
170
results.append("\"");
171
if (accesskey != null) {
172
results.append(" accesskey=\"");
173
results.append(accesskey);
174
results.append("\"");
176
if (tabindex != null) {
177
results.append(" tabindex=\"");
178
results.append(tabindex);
179
results.append("\"");
181
results.append(" value=\"");
182
String value = (this.value == null) ? this.constant : this.value;
185
JspException e = new JspException(messages.getMessage("multiboxTag.value"));
186
pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE);
189
results.append(TagUtils.getInstance().filter(value));
190
results.append("\"");
191
Object bean = TagUtils.getInstance().lookup(pageContext, name, null);
192
String values[] = null;
195
throw new JspException(messages.getMessage("getter.bean", name));
199
values = BeanUtils.getArrayProperty(bean, property);
200
if (values == null) {
201
values = new String[0];
204
} catch (IllegalAccessException e) {
205
throw new JspException(messages.getMessage("getter.access", property, name));
206
} catch (InvocationTargetException e) {
207
Throwable t = e.getTargetException();
208
throw new JspException(messages.getMessage("getter.result", property, t.toString()));
209
} catch (NoSuchMethodException e) {
210
throw new JspException(messages.getMessage("getter.method", property, name));
213
for (int i = 0; i < values.length; i++) {
214
if (value.equals(values[i])) {
215
results.append(" checked=\"checked\"");
220
results.append(prepareEventHandlers());
221
results.append(prepareStyles());
222
results.append(getElementClose());
224
TagUtils.getInstance().write(pageContext, results.toString());
230
* Release any acquired resources.
232
public void release() {
236
name = Constants.BEAN_KEY;