日期:2014-05-16  浏览次数:20403 次

easyui tree使用struts2返回的json数据

最近在用jQuery就在网上找了个树js来用,发现easyui所以就配置一下使用,下载了论坛上的一个离线doc文档谁知道那个文档的属性不全,easyui属性只写了两个(url和animate),那就用吧,可是问题来了,struts2返回的json数据格式是key:value型的怎么转啊,找了半天,后来在一个问题贴上看到有一个data属性晕,上官方网站看了下.

NameTypeDescriptionDefault
url string a URL to retrive remote data. null
animate boolean Defines if to show animation effect when node expand or collapse. false
checkbox boolean Defines if to show the checkbox before every node. false
cascadeCheck boolea Defines if to cascade check. true
onlyLeafCheck boolean Defines if to show the checkbox only before leaf node. false
data array The node data to be loaded. null

里面有一个data属性就是需要传入的node data.

后台类


package com.eversun.delivery.web.action.site;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.eversun.delivery.database.ISiteManager;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class FindSiteTreeAction extends ActionSupport implements
		ServletResponseAware {
	@Resource
	private ISiteManager siteManager;

	private HttpServletResponse response;
        //字符串返回
	private String treeJsonStr;
        //开始我使用的ArrayList传递过去可是怎么也出不来数据改成string了.
       //后来用这个试验也可以
	private JSONArray jsonList ;
	
	@JSON(serialize = false)
	public String getTreeList() {
                 //这句话不用理会就是返回一个jsonarray就行
		JSONArray jsonArray = (JSONArray)siteManager.getTreeJsonData(siteManager
				.getProvinceAndSiteRoot());
		this.setJsonList(jsonArray);
		this.setTreeJsonStr(jsonArray.toString());
		return SUCCESS;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;

	}

	public String getTreeJsonStr() {
		return treeJsonStr;
	}

	public void setTreeJsonStr(String treeJsonStr) {
		this.treeJsonStr = treeJsonStr;
	}

	public JSONArray getJsonList() {
		return jsonList;
	}

	public void setJsonList(JSONArray jsonList) {
		this.jsonList = jsonList;
	}
	

}

?前台代码

<%@page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<head>
<style>
.layoutstyle{
height:100%;
width:100%;
display:block;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>网点管理</title>
<link rel="stylesheet" type="text/css" href="../../css/easyui.css">
<script type="text/javascript" src="../../js/common/jquery.js"></script>
<script type="text/javascript" src="../../js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript">
jQuery(function(){
$.getJSON("getSiteTreeAction.action", function(json){
	//alert(json.jsonList.length);
        //string 的需要这样转一下由于data的数据类型是array的.
	var strjson = eval('('+json.treeJsonStr+')');
        //alert(strjson.jsonList.length);

	$('#typetree').tree({
	checkbox: false,
        //这里需要注意一下data一共可以传两种方式的数据第一种被我注释掉了
	//data:json.jsonList, 
	data:strjson,