第一种方法:

js: 

<script type="text/javascript">
	$(function() {
		$(document).ready(function() {

			$.post("./test/tree.action", {}, function(json) {

				$("#tt").tree({
					data : json.itemsList,
					onClick : function(node) {
						$.post("./test/tree.action", {
							"id" : node.id
						}, function(json) {
							$('#tt').tree('append', {
								parent : node.target,
								data : json.data
							});
						}, "json");

					}
				});
			}, "json");
		});
	});
</script>
</head>
<body>
	<ul id="tt"></ul>
</body>

struts:

action:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionSupport;

public class TreeAction extends ActionSupport {
	private List<Map<String, Object>> itemsList;
	
	private int id;

	@Override
	public String execute() throws Exception {
		
		itemsList = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < 5; i++) {
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("id", i);
			item.put("text", i);
			item.put("state", "closed");
		//	item.put("children", itemsList2);
			itemsList.add(item);
		}

		return super.execute();
	}

	@JSON(serialize = true, name = "data")
	public List<Map<String, Object>> getItemsList() {
		return itemsList;
	}

	public void setItemsList(List<Map<String, Object>> itemsList) {
		this.itemsList = itemsList;
	}

}
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>

	<package name="test" extends="json-default" namespace="/test">
		<action name="tree"
			class="com.test.struts.TreeAction">
			<result name="success" type="json"></result>
		</action>
	</package>

</struts>    


第二种方法:

js:

<script type="text/javascript">
	$(function() {
		$(document).ready(function() {
			$("#tt2").tree({
				url : "test/tree.action"
			});

		});

	});
</script>
</head>
<body>
	<ul id="tt2"></ul>
</body>

struts:

public class TreeAction extends ActionSupport {

	private int id;

	@Override
	public String execute() throws Exception {
		//可根据id查找相应节点

		itemsList = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < 5; i++) {
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("id", i);
			item.put("text", i);
			item.put("state", "closed");
			// item.put("children", itemsList2);
			itemsList.add(item);
		}

		String data = JSONUtils.toJson(itemsList).toString();

		ServletActionContext.getResponse().getWriter().write(data);
		
		return null;
	}
//getter
//setter
}


GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 4 个月前
8c391e04 7 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐