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