1.先导入依赖

<!-- springmvc使用@responseBody  start-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.3</version>
</dependency>
<!-- springmvc使用@responseBody end-->

2.Controller代码

//返回数据的json格式:  [{ id:2, pId:0, name:"系统管理", checked:true, open:true}]
@RequestMapping("/genzTreeNodes")
@ResponseBody
public List<HashMap<String, Object>> genzTreeNodes(Role role){ List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); //遍历模块列表
List<Module> moduleList = moduleService.find()//获取所有模块列表 for (Module module : moduleList) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("id", module.getId());
//添加字符串
map.put("pId", module.getParentId());
map.put("name", module.getName());
//添加布尔类型
map.put("checked", true); data.add(map);
}
//返回数据
return data;
}
05-23 20:18