1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.interceptor;
23
24 import com.opensymphony.xwork2.ActionInvocation;
25 import com.opensymphony.xwork2.interceptor.Interceptor;
26
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.HashMap;
30 import java.util.Iterator;
31
32 /***
33 * <!-- START SNIPPET: description -->
34 * Looks for a hidden identification field that specifies the original value of the checkbox.
35 * If the checkbox isn't submitted, insert it into the parameters as if it was with the value
36 * of 'false'.
37 * <!-- END SNIPPET: description -->
38 * <p/>
39 * <!-- START SNIPPET: parameters -->
40 * <ul><li>setUncheckedValue -
41 * The default value of an unchecked box can be overridden by setting the 'uncheckedValue' property.
42 * </li></ul>
43 * <!-- END SNIPPET: parameters -->
44 * <p/>
45 * <!-- START SNIPPET: extending -->
46 * <p/>
47 * <!-- END SNIPPET: extending -->
48 */
49 public class CheckboxInterceptor implements Interceptor {
50
51 /*** Auto-generated serialization id */
52 private static final long serialVersionUID = -586878104807229585L;
53
54 private String uncheckedValue = Boolean.FALSE.toString();
55
56 public void destroy() {
57 }
58
59 public void init() {
60 }
61
62 public String intercept(ActionInvocation ai) throws Exception {
63 Map parameters = ai.getInvocationContext().getParameters();
64 Map<String, String> newParams = new HashMap<String, String>();
65 Set<String> keys = parameters.keySet();
66 for (Iterator<String> iterator = keys.iterator(); iterator.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
75 if (!parameters.containsKey(name)) {
76
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 }