Ztree如何应用与javaWeb项目、发送action请求返回json
js方法://点击Ztree 实践 发送请求function querZtree(){
$.ajax({
url:'baseInfoAction_queryZtree.action',
async:false,
data:{'time':(new Date()).toString()},
success:function(result){
var array = eval(result);
var setting = {
view: {
showIcon: false,
showLine: false
},
callback : {
onClick: zTreeOnClick//回调方法 用户点击 传参数
}
};
$.fn.zTree.init($("#treeBox"), setting, array);//把数据放到 treeBox内
}
});
}Ztree回调方法://点击知识点 获取对象
function zTreeOnClick(event, treeId, treeNode){
alert(treeNode.name)
struts.xml 配置文件:
<action name="baseInfoAction_*" method="{1}" class="com.53itstyle.web.baseInfo.action.BaseInfoAction">
<result name="knowledge"></result>
</action>Action 后台方法:
public void queryZtree(){
try {
JSONArray resultArray = new JSONArray();
for(int i=0;i<4;i++){
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "一级"+i);
jsonObject.put("open", true);
JSONArray typeArr = new JSONArray();
for(int j=0;j<4;j++){
JSONObject knObj = new JSONObject();
knObj.put("name", "二级"+j);
knObj.put("open", true);
typeArr.put(knObj);
jsonObject.put("children", typeArr);
}
resultArray.put(jsonObject);
}
printMsgToClient(resultArray.toString());
} catch (Exception e) {
e.printStackTrace();
}
}将结果返回给xmlRequest:
/**
* 将结果返回给xmlRequest
*
* @param s
* @param dictList
* @return
* @throws Exception
*/
public void printMsgToClient(String result) throws Exception {
getResponse().setCharacterEncoding("UTF-8");
PrintWriter out = getResponse().getWriter();
try {
out.print(result);
}catch (Exception e) {
e.printStackTrace();
} finally {
out.close();
}
}
很不错哟,谢谢楼主,受教了。
页:
[1]