1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
65 if (!parameters.containsKey(name)) {
66
67
68 newParams.put(name, new String[0]);
69 }
70 }
71 }
72
73 parameters.putAll(newParams);
74
75 return actionInvocation.invoke();
76 }
77 }