struts2中的业务层,如何传一个集合给jquery并显示成树形 如何配置,需要代码,注释。高手帮忙~~~~初学~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把树结构作成json格式发给页面,然后在页面上用js解析。用jQuery和相应的插件,这个不太难搞的。 菜单可以用struts2+extjs+json来实现,下面是个简单的例子1.定义菜单类package com.lab;import java.util.List;public class Menu { private int id; private String text; private boolean leaf; private String cls; private List<Menu> children; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public boolean isLeaf() { return leaf; } public void setLeaf(boolean leaf) { this.leaf = leaf; } public String getCls() { return cls; } public void setCls(String cls) { this.cls = cls; } public List<Menu> getChildren() { return children; } public void setChildren(List<Menu> children) { this.children = children; }}2.定义生成菜单的action,向客户端返回json格式的数据package com.lab;import java.util.ArrayList;import java.util.List;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import net.sf.json.JSONArray;public class MenuAction { private static final Log LOG=LogFactory.getLog(MenuAction.class); private String menuString; private List<Menu> menus; public String execute() { menus = new ArrayList<Menu>(); Menu benz = new Menu(); benz.setText("Benz"); benz.setCls("folder"); benz.setLeaf(false); benz.setId(10); menus.add(benz); List<Menu> benzList = new ArrayList<Menu>(); benz.setChildren(benzList); Menu menu; menu = new Menu(); menu.setText("S600"); menu.setCls("file"); menu.setLeaf(true); menu.setId(11); benzList.add(menu); menu = new Menu(); menu.setText("SLK200"); menu.setCls("file"); menu.setLeaf(true); menu.setId(12); benzList.add(menu); Menu bmw = new Menu(); bmw.setText("BMW"); bmw.setCls("folder"); bmw.setLeaf(false); bmw.setId(20); menus.add(bmw); List<Menu> bmwList = new ArrayList<Menu>(); bmw.setChildren(bmwList); menu = new Menu(); menu.setText("325i"); menu.setCls("file"); menu.setLeaf(true); menu.setId(21); bmwList.add(menu); menu = new Menu(); menu.setText("X5"); menu.setCls("file"); menu.setLeaf(true); menu.setId(22); bmwList.add(menu); JSONArray jsonObject = JSONArray.fromObject(menus); try { menuString = jsonObject.toString(); } catch (Exception e) { menuString = "ss"; } LOG.info(menuString); return "success"; } public String getMenuString() { return menuString; } public void setMenuString(String menuString) { this.menuString = menuString; }}3.修改struts.xml文件<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <package name="person" extends="struts-default"> <action name="menus" method="execute" class="com.lab.MenuAction"> <result>/menu.jsp</result></action> <action name="fileUpload" class="com.lab.UploadAction"></action></package> <constant name="struts.devMode" value="true"></constant></struts> 4.编写menu.jsp,这个页面主要是用来把action的输出传递给请求页面。如果用struts2的json插件的话就不需要这个页面。<%@ taglib prefix="s" uri="/struts-tags" %> <s:property value="menuString" escape="false"/> 5.请求页面<body> <script type="text/javascript">/* * Ext JS Library 2.3.0 * Copyright(c) 2006-2009, Ext JS, LLC. * [email protected] * * http://extjs.com/license */Ext.onReady(function(){ // shorthand var Tree = Ext.tree; var tree = new Tree.TreePanel({ el:'tree-div', useArrows:true, autoScroll:true, animate:true, enableDD:true, containerScroll: true, // auto create TreeLoader dataUrl: 'http://localhost:10000/extjs_tree/menus.action', root: { nodeType: 'async', text: 'Ext JS', draggable:false, id:'source' } }); // render the tree tree.render(); tree.getRootNode().expand();});</script> <div id="tree-div" style="overflow:auto; height:300px;width:250px;border:1px solid #c3daf9;"></div> </body> hibernate 注解一个“分类”bean hibarnate的问题 struts2中使用普通表单怎么样提交到Action? Spring 3.0 发行包里的xsd文件在哪 200元求个毕业论文 FormBean怎么去对应页面的多选框(select) J2EE有没有比较好权限解决方案? 用Spring,同时对数据库和XML操作,怎样进行事务控制 在Eclipse中通过Lomboz-J2EE wizard创建的Project中创建JSP文件报错!!真郁闷 tomcat又无法启动了 请前辈指教怎么考Java的认证 乱码问题,请高手指教
用jQuery和相应的插件,这个不太难搞的。
1.定义菜单类package com.lab;import java.util.List;public class Menu {
private int id;
private String text;
private boolean leaf;
private String cls;
private List<Menu> children; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getText() {
return text;
} public void setText(String text) {
this.text = text;
} public boolean isLeaf() {
return leaf;
} public void setLeaf(boolean leaf) {
this.leaf = leaf;
} public String getCls() {
return cls;
} public void setCls(String cls) {
this.cls = cls;
} public List<Menu> getChildren() {
return children;
} public void setChildren(List<Menu> children) {
this.children = children;
}
}2.定义生成菜单的action,向客户端返回json格式的数据package com.lab;import java.util.ArrayList;
import java.util.List;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;import net.sf.json.JSONArray;public class MenuAction {
private static final Log LOG=LogFactory.getLog(MenuAction.class);
private String menuString; private List<Menu> menus; public String execute() { menus = new ArrayList<Menu>(); Menu benz = new Menu();
benz.setText("Benz");
benz.setCls("folder");
benz.setLeaf(false);
benz.setId(10);
menus.add(benz); List<Menu> benzList = new ArrayList<Menu>();
benz.setChildren(benzList); Menu menu;
menu = new Menu();
menu.setText("S600");
menu.setCls("file");
menu.setLeaf(true);
menu.setId(11);
benzList.add(menu);
menu = new Menu();
menu.setText("SLK200");
menu.setCls("file");
menu.setLeaf(true);
menu.setId(12);
benzList.add(menu); Menu bmw = new Menu();
bmw.setText("BMW");
bmw.setCls("folder");
bmw.setLeaf(false);
bmw.setId(20);
menus.add(bmw); List<Menu> bmwList = new ArrayList<Menu>();
bmw.setChildren(bmwList); menu = new Menu();
menu.setText("325i");
menu.setCls("file");
menu.setLeaf(true);
menu.setId(21);
bmwList.add(menu); menu = new Menu();
menu.setText("X5");
menu.setCls("file");
menu.setLeaf(true);
menu.setId(22);
bmwList.add(menu); JSONArray jsonObject = JSONArray.fromObject(menus);
try {
menuString = jsonObject.toString();
} catch (Exception e) {
menuString = "ss";
}
LOG.info(menuString);
return "success";
} public String getMenuString() {
return menuString;
} public void setMenuString(String menuString) {
this.menuString = menuString;
}
}
3.修改struts.xml文件<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<package name="person" extends="struts-default">
<action name="menus" method="execute" class="com.lab.MenuAction">
<result>/menu.jsp</result></action>
<action name="fileUpload" class="com.lab.UploadAction"></action></package>
<constant name="struts.devMode" value="true"></constant></struts>
4.编写menu.jsp,这个页面主要是用来把action的输出传递给请求页面。如果用struts2的json插件的话就不需要这个页面。<%@ taglib prefix="s" uri="/struts-tags" %>
<s:property value="menuString" escape="false"/>
5.请求页面<body>
<script type="text/javascript">
/*
* Ext JS Library 2.3.0
* Copyright(c) 2006-2009, Ext JS, LLC.
* [email protected]
*
* http://extjs.com/license
*/Ext.onReady(function(){
// shorthand
var Tree = Ext.tree;
var tree = new Tree.TreePanel({
el:'tree-div',
useArrows:true,
autoScroll:true,
animate:true,
enableDD:true,
containerScroll: true, // auto create TreeLoader
dataUrl: 'http://localhost:10000/extjs_tree/menus.action', root: {
nodeType: 'async',
text: 'Ext JS',
draggable:false,
id:'source'
}
}); // render the tree
tree.render();
tree.getRootNode().expand();
});
</script>
<div id="tree-div" style="overflow:auto; height:300px;width:250px;border:1px solid #c3daf9;"></div>
</body>