View Javadoc

1   /*
2    * $Id: BackgroundProcess.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  package org.apache.struts2.interceptor;
22  
23  
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import com.opensymphony.xwork2.ActionInvocation;
30  import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
31  
32  
33  /***
34   * Just as the CheckboxInterceptor checks that if only the hidden field is present, so too does this interceptor. If
35   * the "__multiselect_" request parameter is present and its visible counterpart is not, set a new request parameter to an
36   * empty Sting.
37   */
38  public class MultiselectInterceptor extends AbstractInterceptor {
39      private static final long serialVersionUID = 1L;
40  
41      /***
42       * Just as the CheckboxInterceptor checks that if only the hidden field is present, so too does this interceptor.
43       * If the "__multiselect_" request parameter is present and its visible counterpart is not, set a new request parameter
44       * to an empty Sting.
45       *
46       * @param actionInvocation ActionInvocation
47       * @return the result of the action
48       * @throws Exception if error
49       * @see com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
50       */
51      public String intercept(ActionInvocation actionInvocation) throws Exception {
52          Map parameters = actionInvocation.getInvocationContext().getParameters();
53          Map<String, Object> newParams = new HashMap<String, Object>();
54          Set<String> keys = parameters.keySet();
55  
56          for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();) {
57              String key = iterator.next();
58  
59              if (key.startsWith("__multiselect_")) {
60                  String name = key.substring("__multiselect_".length());
61  
62                  iterator.remove();
63  
64                  // is this multi-select box submitted?
65                  if (!parameters.containsKey(name)) {
66  
67                      // if not, let's be sure to default the value to an empty string array
68                      newParams.put(name, new String[0]);
69                  }
70              }
71          }
72  
73          parameters.putAll(newParams);
74  
75          return actionInvocation.invoke();
76      }
77  }