1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.components;
19
20 import java.lang.reflect.Array;
21 import java.util.Collection;
22 import java.util.Map;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.apache.struts2.util.ContainUtil;
28 import org.apache.struts2.util.MakeIterator;
29
30 import com.opensymphony.xwork2.util.ValueStack;
31
32 /***
33 * DoubleListUIBean is the standard superclass of all Struts list handling components.
34 *
35 * <p/>
36 *
37 * <!-- START SNIPPET: javadoc -->
38 *
39 * Note that the listkey and listvalue attribute will default to "key" and "value"
40 * respectively only when the list attribute is evaluated to a Map or its decendant.
41 * Other thing else, will result in listkey and listvalue to be null and not used.
42 *
43 * <!-- END SNIPPET: javadoc -->
44 *
45 */
46 public abstract class ListUIBean extends UIBean {
47 protected Object list;
48 protected String listKey;
49 protected String listValue;
50
51
52 protected boolean throwExceptionOnNullValueAttribute = false;
53
54 protected ListUIBean(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
55 super(stack, request, response);
56 }
57
58 public void evaluateExtraParams() {
59 Object value = null;
60
61 if (list == null) {
62 list = parameters.get("list");
63 }
64
65 if (list instanceof String) {
66 value = findValue((String) list);
67 } else if (list instanceof Collection) {
68 value = list;
69 } else if (MakeIterator.isIterable(list)) {
70 value = MakeIterator.convert(list);
71 }
72 if (value == null) {
73 if (throwExceptionOnNullValueAttribute) {
74
75 value = findValue((list == null) ? (String) list : list.toString(), "list",
76 "The requested list key '" + list + "' could not be resolved as a collection/array/map/enumeration/iterator type. " +
77 "Example: people or people.{name}");
78 }
79 else {
80
81
82 value = findValue((list == null)?(String) list:list.toString());
83 }
84 }
85
86 if (value instanceof Collection) {
87 addParameter("list", value);
88 } else {
89 addParameter("list", MakeIterator.convert(value));
90 }
91
92 if (value instanceof Collection) {
93 addParameter("listSize", new Integer(((Collection) value).size()));
94 } else if (value instanceof Map) {
95 addParameter("listSize", new Integer(((Map) value).size()));
96 } else if (value != null && value.getClass().isArray()) {
97 addParameter("listSize", new Integer(Array.getLength(value)));
98 }
99
100 if (listKey != null) {
101 addParameter("listKey", listKey);
102 } else if (value instanceof Map) {
103 addParameter("listKey", "key");
104 }
105
106 if (listValue != null) {
107 if (altSyntax()) {
108
109
110 if (listValue.startsWith("%{") && listValue.endsWith("}")) {
111 listValue = listValue.substring(2, listValue.length() - 1);
112 }
113 }
114 addParameter("listValue", listValue);
115 } else if (value instanceof Map) {
116 addParameter("listValue", "value");
117 }
118 }
119
120 public boolean contains(Object obj1, Object obj2) {
121 return ContainUtil.contains(obj1, obj2);
122 }
123
124 protected Class getValueClassType() {
125 return null;
126 }
127
128 /***
129 * Iterable source to populate from. If the list is a Map (key, value), the Map key will become the option "value" parameter and the Map value will become the option body.
130 * @s.tagattribute required="true"
131 */
132 public void setList(Object list) {
133 this.list = list;
134 }
135
136 /***
137 * Property of list objects to get field value from
138 * @s.tagattribute required="false"
139 */
140 public void setListKey(String listKey) {
141 this.listKey = listKey;
142 }
143
144 /***
145 * Property of list objects to get field content from
146 * @s.tagattribute required="false"
147 */
148 public void setListValue(String listValue) {
149 this.listValue = listValue;
150 }
151
152
153 public void setThrowExceptionOnNullValueAttribute(boolean throwExceptionOnNullValueAttribute) {
154 this.throwExceptionOnNullValueAttribute = throwExceptionOnNullValueAttribute;
155 }
156 }