View Javadoc
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   * <p>
21   * 创建一个菜单列表,详见 <a target="_blank" href="http://api.jqueryui.com/menu/">Menu Widget</a>
22   * </p>
23   * <p>
24   * 示例代码:
25   * <pre>
26   * &lt;g2:menu id="menuWithItems"&gt;
27   *     &lt;g2:menuItem title="广联达软件股份有限公司" href="http://www.glodon.com/" /&gt;
28   *     &lt;g2:menuItem title="广联达正源兴邦科技有限公司" href="http://www.grandsoft.com.cn" /&gt;
29   *     &lt;g2:menuItem title="公司介绍"&gt;
30   *         &lt;g2:menu id="subMenuPlugins"&gt;
31   *             &lt;g2:menuItem title="公司简介" href="http://www.glodon.com/aboutus/glodon/intro" /&gt;
32   *             &lt;g2:menuItem title="公司荣誉" href="http://www.glodon.com/infos?type=5" /&gt;
33   *         &lt;/g2:menu&gt;
34   *     &lt;/g2:menuItem&gt;
35   *     &lt;g2:menuItem title="广联达产品"&gt;
36   *         &lt;g2:menu id="subMenuSocialMedia"&gt;
37   *             &lt;g2:menuItem title="建设业主项目管理系统" href="http://www.glodon.com/products/80" /&gt;
38   *             &lt;g2:menuItem title="企业操作系统" href="http://www.glodon.com/products/74" /&gt;
39   *         &lt;/g2:menu&gt;
40   *     &lt;/g2:menuItem&gt;
41   *     &lt;g2:menuItem title="AJAX"&gt;
42   *         &lt;g2:menu id="subMenuAjax"&gt;
43   *             &lt;g2:menuItem title="Ajax1"&gt;
44   *                 &lt;g2:remote href="ajax/example.action?forward=ajax/ajax1" targets="result" /&gt;
45   *             &lt;/g2:menuItem&gt;
46   *             &lt;g2:menuItem title="Ajax 2"&gt;
47   *                 &lt;g2:remote href="ajax/example.action?forward=ajax/ajax2" targets="result" effect="highlight" effectDuration="2500"/&gt;
48   *             &lt;/g2:menuItem&gt;
49   *             &lt;g2:menuItem title="Ajax 3"&gt;
50   *                 &lt;g2:remote href="ajax/example.action?forward=ajax/ajax3" targets="result" /&gt;
51   *                 &lt;g2:topic onBeforeTopics="beforeLink" onCompleteTopics="completeLink" /&gt;
52   *             &lt;/g2:menuItem&gt;
53   *         &lt;/g2:menu&gt;
54   *     &lt;/g2:menuItem&gt;
55   * &lt;/g2:menu&gt;
56   * 
57   * &lt;p&gt;
58   *     &lt;strong&gt;Result Div :&lt;/strong&gt;
59   *     &lt;div id="result" class="result ui-widget-content ui-corner-all"&gt;Click on the AJAX Links above.&lt;/div&gt;
60   * &lt;/p&gt;
61   * </pre>
62   * </p>
63   * 
64   * @see MenuItem
65   * @author <a href="mailto:[email protected]">何明旺</a>
66   * @since 3.0
67   * @date 2014年6月10日
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                 // will throw an exception if not found
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 }