01: /*
02: * $Id: CheckboxInterceptor.java 478625 2006-11-23 17:31:52Z wsmoak $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21: package org.apache.struts2.interceptor;
22:
23: import com.opensymphony.xwork2.ActionInvocation;
24: import com.opensymphony.xwork2.interceptor.Interceptor;
25:
26: import java.util.Map;
27: import java.util.Set;
28: import java.util.HashMap;
29: import java.util.Iterator;
30:
31: /**
32: * <!-- START SNIPPET: description -->
33: * Looks for a hidden identification field that specifies the original value of the checkbox.
34: * If the checkbox isn't submitted, insert it into the parameters as if it was with the value
35: * of 'false'.
36: * <!-- END SNIPPET: description -->
37: * <p/>
38: * <!-- START SNIPPET: parameters -->
39: * <ul><li>setUncheckedValue -
40: * The default value of an unchecked box can be overridden by setting the 'uncheckedValue' property.
41: * </li></ul>
42: * <!-- END SNIPPET: parameters -->
43: * <p/>
44: * <!-- START SNIPPET: extending -->
45: * <p/>
46: * <!-- END SNIPPET: extending -->
47: */
48: public class CheckboxInterceptor implements Interceptor {
49:
50: /** Auto-generated serialization id */
51: private static final long serialVersionUID = -586878104807229585L;
52:
53: private String uncheckedValue = Boolean.FALSE.toString();
54:
55: public void destroy() {
56: }
57:
58: public void init() {
59: }
60:
61: public String intercept(ActionInvocation ai) throws Exception {
62: Map parameters = ai.getInvocationContext().getParameters();
63: Map<String, String> newParams = new HashMap<String, String>();
64: Set<String> keys = parameters.keySet();
65: for (Iterator<String> iterator = keys.iterator(); iterator
66: .hasNext();) {
67: String key = iterator.next();
68:
69: if (key.startsWith("__checkbox_")) {
70: String name = key.substring("__checkbox_".length());
71:
72: iterator.remove();
73:
74: // is this checkbox checked/submitted?
75: if (!parameters.containsKey(name)) {
76: // if not, let's be sure to default the value to false
77: newParams.put(name, uncheckedValue);
78: }
79: }
80: }
81:
82: parameters.putAll(newParams);
83:
84: return ai.invoke();
85: }
86:
87: /**
88: * Overrides the default value for an unchecked checkbox
89: *
90: * @param uncheckedValue The uncheckedValue to set
91: */
92: public void setUncheckedValue(String uncheckedValue) {
93: this.uncheckedValue = uncheckedValue;
94: }
95: }
|