View Javadoc

1   /*
2    * $Id: CheckboxInterceptor.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "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  
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                  // 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  }