日期:2014-05-16 浏览次数:20328 次
核心提示:最近尝试用extjs来展示树状菜单。着实花了一番功夫。树状菜单的菜单项需要动态加载,而目前版本的extjs中只支持JSON格式的数据。查了一些资料,决定使用struts2的json-plugin。首先按照例子做了一个,但是结果就是不成功,界面上只出来了一个js中生成的root |
{"cls":"folder","id":10,"leaf":false,"children":[{"cls":"file","id":11,"leaf":true,"children":null,"text":"S600"},{"cls":"file","id":12,"leaf":true,"children":null,"text":"SLK200"}],"text":"Benz"}
[{"cls":"folder","id":10,"leaf":false,"children":[{"cls":"file","id":11,"leaf":true,"children":null,"text":"S600"},{"cls":"file","id":12,"leaf":true,"children":null,"text":"SLK200"}],"text":"Benz"}]
1. <?xml version="1.0" encoding="UTF-8"?> 2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 6. <welcome-file-list> 7. <welcome-file>index.jsp</welcome-file> 8. </welcome-file-list> 9. <filter> 10. <filter-name>struts2</filter-name> 11. <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 12. </filter> 13. 14. <filter-mapping> 15. <filter-name>struts2</filter-name> 16. <url-pattern>/*</url-pattern> 17. </filter-mapping> 18. </web-app> 19.
1. <?xml version="1.0" encoding="UTF-8"?> 2. <!DOCTYPE struts PUBLIC 3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4. "http://struts.apache.org/dtds/struts-2.0.dtd"> 5. 6. <struts> 7. <constant name="struts.devMode" value="true"/> 8. <constant name="struts.i18n.encoding" value="UTF-8"/> 9. <package name="person" extends="struts-default"> 10. <action name="menus" method="execute" class="com.lab.MenuAction"> 11. <result>/menu.jsp</result> 12. </action> 13. </package> 14. </struts>
1. public class Menu { 2. private int id; 3. private String text; 4. private boolean leaf; 5. private String cls; 6. private List<Menu> children; 7. }
1. package com.lab; 2. 3. import java.util.ArrayList; 4. import java.util.List; 5. 6. import net.sf.json.JSONArray; 7. 8. public class MenuAction { 9. priv