1 package gboat2.base.view.components;
2
3 import java.lang.reflect.Array;
4 import java.util.Collection;
5 import java.util.Map;
6
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import org.apache.commons.lang3.RandomStringUtils;
11 import org.apache.commons.lang3.StringUtils;
12 import org.apache.struts2.components.ClosingUIBean;
13 import org.apache.struts2.util.MakeIterator;
14 import org.apache.struts2.views.annotations.StrutsTag;
15 import org.apache.struts2.views.annotations.StrutsTagAttribute;
16
17 import com.opensymphony.xwork2.util.ValueStack;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 @StrutsTag(
70 name = "menu",
71 tldTagClass = "gboat2.base.view.jsp.MenuTag",
72 description = "创建一个菜单列表,详见 http://api.jqueryui.com/menu/")
73 public class Menu extends ClosingUIBean {
74 public static final String JQUERYACTION = "menu";
75
76 public static final String TEMPLATE = "menu";
77 public static final String TEMPLATE_CLOSE = "menu-close";
78
79 protected boolean throwExceptionOnNullValueAttribute = false;
80
81 protected String targets;
82 protected String href;
83 protected String paramName;
84 protected Object list;
85 protected String listKey;
86 protected String listValue;
87
88 public Menu(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
89 super(stack, request, response);
90 }
91
92 @Override
93 public String getDefaultOpenTemplate() {
94 return TEMPLATE;
95 }
96
97 @Override
98 protected String getDefaultTemplate() {
99 return TEMPLATE_CLOSE;
100 }
101
102 @SuppressWarnings("rawtypes")
103 @Override
104 public void evaluateExtraParams() {
105 super.evaluateExtraParams();
106
107 addParameter("jqueryaction", JQUERYACTION);
108
109 if (targets != null)
110 addParameter("targets", findString(targets));
111
112 if (href != null)
113 addParameter("href", findString(href));
114
115 if (paramName != null)
116 addParameter("paramName", findString(paramName));
117
118 Object value = null;
119
120 if (list == null) {
121 list = parameters.get("list");
122 }
123
124 if (list instanceof String) {
125 value = findValue((String) list);
126 } else if (list instanceof Collection) {
127 value = list;
128 } else if (MakeIterator.isIterable(list)) {
129 value = MakeIterator.convert(list);
130 }
131
132 if (value == null) {
133 if (throwExceptionOnNullValueAttribute) {
134
135 value = findValue((list == null) ? (String) list : list.toString(),
136 "list",
137 "The requested list key '" + list + "' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name}");
138 } else {
139 value = findValue((list == null) ? (String) list : list.toString());
140 }
141 }
142
143 if (value instanceof Collection) {
144 addParameter("list", value);
145 } else {
146 addParameter("list", MakeIterator.convert(value));
147 }
148
149 if (value instanceof Collection) {
150 addParameter("listSize", Integer.valueOf(((Collection) value).size()));
151 } else if (value instanceof Map) {
152 addParameter("listSize", Integer.valueOf(((Map) value).size()));
153 } else if (value != null && value.getClass().isArray()) {
154 addParameter("listSize", Integer.valueOf(Array.getLength(value)));
155 }
156
157 if (listKey != null) {
158 listKey = stripExpressionIfAltSyntax(listKey);
159 addParameter("listKey", listKey);
160 } else if (value instanceof Map) {
161 addParameter("listKey", "key");
162 }
163
164 if (listValue != null) {
165 listValue = stripExpressionIfAltSyntax(listValue);
166 addParameter("listValue", listValue);
167 } else if (value instanceof Map) {
168 addParameter("listValue", "value");
169 }
170
171 if (StringUtils.isBlank(id)) {
172 this.id = "menu_" + RandomStringUtils.randomNumeric(5);
173 addParameter("id", this.id);
174 }
175
176 Menu parentMenu = (Menu) findAncestor(Menu.class);
177 if (parentMenu != null) {
178 addParameter("subMenu", true);
179 addParameter("parentMenu", findString(parentMenu.getId()));
180 }
181 }
182
183 @Override
184 @StrutsTagAttribute(description = "如果设置为 true,则禁用该菜单", type = "Boolean", defaultValue = "false")
185 public void setDisabled(String disabled) {
186 this.disabled = disabled;
187 }
188
189 @StrutsTagAttribute(description = "显示菜单项所指向内容的 DOM 节点的 ID,多值之间使用逗号分隔")
190 public void setTargets(String targets) {
191 this.targets = targets;
192 }
193
194 @StrutsTagAttribute(description = "从一个已有列表(集合)加载菜单项,该列表可以是 Array/Map/Collection 等 Iterator 对象")
195 public void setList(Object list) {
196 this.list = list;
197 }
198
199 @StrutsTagAttribute(description = "从一个列表中加载菜单项时,菜单项链接地址后面跟的参数的值对应的字段名称")
200 public void setListKey(String listKey) {
201 this.listKey = listKey;
202 }
203
204 @StrutsTagAttribute(description = "从一个列表中加载菜单项时,菜单项显示的文字对应的字段名称")
205 public void setListValue(String listValue) {
206 this.listValue = listValue;
207 }
208
209 @StrutsTagAttribute(description = "从一个列表中加载菜单项时,菜单项的链接地址")
210 public void setHref(String href) {
211 this.href = href;
212 }
213
214 @StrutsTagAttribute(description = "从一个列表中加载菜单项时,菜单项链接地址后面跟的参数名称,如: id、name", defaultValue = "id")
215 public void setParamName(String paramName) {
216 this.paramName = paramName;
217 }
218 }