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