<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">package com.qingyuan.huake;</span>
public class Human {
private int id;
private int pId;
private String name;
private boolean isOpen;
public Human()
{
}
public Human(int id, int pId, String name, boolean isOpen)
{
this.id = id;
this.pId = pId;
this.name = name;
this.isOpen = isOpen;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getpId() {
return pId;
}
public void setpId(int pId) {
this.pId = pId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isOpen() {
return isOpen;
}
public void setOpen(boolean isOpen) {
this.isOpen = isOpen;
}
}
package com.qingyuan.huake;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class JsonWriter extends HttpServlet {
private static final long serialVersionUID = -437186441730421901L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// String str1 = new String(request.getParameter("params").getBytes("ISO-8859-1"),"gb2312");
this.doPost(request, response);
}
/**
* Gson除了提供原本的Json相关功能外,更包含了Json和Java Object的互相转换,
* 而且许多常见的类型也可以转换,特別是collection集合,非常方便
*
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
Human human1 = new Human(1, 0, "北京市 1",true);
Human human11 = new Human(11, 1, "海定區 1-1",false);
Human human12 = new Human(12, 1, "湖北省 1-2",true);
Human human121 = new Human(121, 12, "荊州市 1-2-1",true);
Human human122 = new Human(122, 12, "鄂州市 1-2-2",true);
Human human2 = new Human(2, 0, "浙江省 2",true);
Human human21 = new Human(21, 2, "杭州市 2-1",false);
Human human22 = new Human(22, 2, "金華市 2-2",true);
Human human221 = new Human(221, 22, "義烏市 2-2-1",true);
Human human222 = new Human(222, 22, "橫店 2-2-2",true);
List<Human> humans = new ArrayList<Human>();
humans.add(human1);humans.add(human11);humans.add(human12);humans.add(human121);humans.add(human122);
humans.add(human2);humans.add(human22);humans.add(human21);humans.add(human222);humans.add(human221);
// 允許有空的對象
Gson gson = new GsonBuilder().serializeNulls().create();
// String str = gson.toJson(human1);
// System.out.print(str);
// {"id":1,"pId":0,"name":"隨意勾選 1","isOpen":true}
String string = gson.toJson(humans);
// json對象數組
// [{"id":1,"pId":0,"name":"隨意勾選 1","isOpen":true},
// {"id":11,"pId":1,"name":"隨意勾選 1-1","isOpen":false},
// {"id":12,"pId":1,"name":"隨意勾選 1-2","isOpen":true},
// {"id":121,"pId":12,"name":"隨意勾選 1-2-1","isOpen":true},
// {"id":122,"pId":12,"name":"隨意勾選 1-2-2","isOpen":true},
// {"id":1,"pId":0,"name":"禁止勾选 2","isOpen":true},
// {"id":11,"pId":1,"name":"禁止勾选 2-1","isOpen":false},
// {"id":12,"pId":1,"name":"禁止勾选 2-2","isOpen":true},
// {"id":11,"pId":1,"name":"禁止勾选 2-1","isOpen":false},
// {"id":122,"pId":12,"name":"禁止勾选 2-2-2","isOpen":true}]
out.write(string);
}
}
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>JsonWriter</servlet-name>
<servlet-class>com.qingyuan.huake.JsonWriter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JsonWriter</servlet-name>
<url-pattern>/json</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.11.2.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.ztree.excheck-3.5.js"></script>
<link rel="stylesheet" href="<%=request.getContextPath() %>/css/demo.css" type="text/css">
<link rel="stylesheet" href="<%=request.getContextPath() %>/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript">
var zNodes = null;
$(function(){
$.ajax({
url:"<%=request.getContextPath()%>/json",
type: "post",
async: false,
dataType:"json",
success: function(result){
zNodes = result;
}
});
$.fn.zTree.init($("#treeDemo"), setting, zNodes);
$("#checkTrue").bind("click", {type:"checkTrue"}, checkNode);
$("#checkFalse").bind("click", {type:"checkFalse"}, checkNode);
$("#toggle").bind("click", {type:"toggle"}, checkNode);
$("#checkTruePS").bind("click", {type:"checkTruePS"}, checkNode);
$("#checkFalsePS").bind("click", {type:"checkFalsePS"}, checkNode);
$("#togglePS").bind("click", {type:"togglePS"}, checkNode);
$("#checkAllTrue").bind("click", {type:"checkAllTrue"}, checkNode);
$("#checkAllFalse").bind("click", {type:"checkAllFalse"}, checkNode);
$("#autoCallbackTrigger").bind("change", {}, setAutoTrigger);
});
var setting = {
view: {
selectedMulti: false
},
check: {
enable: true
},
data: {
simpleData: {
enable: true
}
},
callback: {
beforeCheck: beforeCheck,
onCheck: onCheck
}
};
// var zNodes =[
// { id:1, pId:0, name:"随意勾选 1", open:true},
// { id:11, pId:1, name:"随意勾选 1-1"},
// { id:12, pId:1, name:"随意勾选 1-2", open:true},
// { id:121, pId:12, name:"随意勾选 1-2-1"},
// { id:122, pId:12, name:"随意勾选 1-2-2"},
// { id:2, pId:0, name:"禁止勾选 2", open:true, doCheck:false},
// { id:21, pId:2, name:"禁止勾选 2-1", doCheck:false},
// { id:22, pId:2, name:"禁止勾选 2-2", checked:true, open:true, doCheck:false},
// { id:221, pId:22, name:"禁止勾选 2-2-1", doCheck:false},
// { id:222, pId:22, name:"禁止勾选 2-2-2", checked:true, doCheck:false},
// { id:23, pId:2, name:"禁止勾选 2-3", doCheck:false}
// ];
var code, log, className = "dark";
function beforeCheck(treeId, treeNode) {
className = (className === "dark" ? "":"dark");
showLog("[ "+getTime()+" beforeCheck ] " + treeNode.name );
return (treeNode.doCheck !== false);
}
function onCheck(e, treeId, treeNode) {
showLog("[ "+getTime()+" onCheck ] " + treeNode.name );
}
function showLog(str) {
if (!log) log = $("#log");
log.append("<li class='"+className+"'>"+str+"</li>");
if(log.children("li").length > 6) {
log.get(0).removeChild(log.children("li")[0]);
}
}
function getTime() {
var now= new Date(),
h=now.getHours(),
m=now.getMinutes(),
s=now.getSeconds(),
ms=now.getMilliseconds();
return (h+":"+m+":"+s+ " " +ms);
}
function checkNode(e) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
type = e.data.type,
nodes = zTree.getSelectedNodes();
if (type.indexOf("All")<0 && nodes.length == 0) {
alert("请先选择一个节点");
}
if (type == "checkAllTrue") {
zTree.checkAllNodes(true);
} else if (type == "checkAllFalse") {
zTree.checkAllNodes(false);
} else {
var callbackFlag = $("#callbackTrigger").attr("checked");
for (var i=0, l=nodes.length; i<l; i++) {
if (type == "checkTrue") {
zTree.checkNode(nodes[i], true, false, callbackFlag);
} else if (type == "checkFalse") {
zTree.checkNode(nodes[i], false, false, callbackFlag);
} else if (type == "toggle") {
zTree.checkNode(nodes[i], null, false, callbackFlag);
}else if (type == "checkTruePS") {
zTree.checkNode(nodes[i], true, true, callbackFlag);
} else if (type == "checkFalsePS") {
zTree.checkNode(nodes[i], false, true, callbackFlag);
} else if (type == "togglePS") {
zTree.checkNode(nodes[i], null, true, callbackFlag);
}
}
}
}
function setAutoTrigger(e) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
zTree.setting.check.autoCheckTrigger = $("#autoCallbackTrigger").attr("checked");
$("#autoCheckTriggerValue").html(zTree.setting.check.autoCheckTrigger ? "true" : "false");
}
// $(document).ready(function(){
// $.fn.zTree.init($("#treeDemo"), setting, zNodes);
// $("#checkTrue").bind("click", {type:"checkTrue"}, checkNode);
// $("#checkFalse").bind("click", {type:"checkFalse"}, checkNode);
// $("#toggle").bind("click", {type:"toggle"}, checkNode);
// $("#checkTruePS").bind("click", {type:"checkTruePS"}, checkNode);
// $("#checkFalsePS").bind("click", {type:"checkFalsePS"}, checkNode);
// $("#togglePS").bind("click", {type:"togglePS"}, checkNode);
// $("#checkAllTrue").bind("click", {type:"checkAllTrue"}, checkNode);
// $("#checkAllFalse").bind("click", {type:"checkAllFalse"}, checkNode);
// $("#autoCallbackTrigger").bind("change", {}, setAutoTrigger);
// });
</script>
</head>
<body>
<div class="content_wrap">
<div class="zTreeDemoBackground left">
<ul id="treeDemo" class="ztree"></ul>
</div>
</div>
</body>
</html>
另外: [以下不可以使用,伪代码]
public TreeModel loadTreeModel(Rule arg0)
{
TreeModel treeModel = new TreeModel();
TreeNode root = new TreeNode();
root.setNodeID("0");
root.setCode("0");
root.setParentId("-1");
root.setNodeText("aaa");
root.setTitle("root");
treeModel.addRoot(root);
try
{
TreeNode treeNode = null;
String[] firstLevel = {"bbb","ccc"};
for (int levelNum = 1; levelNum <= firstLevel.length; levelNum++)
{
treeNode = new TreeNode();
treeNode.setNodeID(levelNum+"");
treeNode.setCode(levelNum+"");
treeNode.setParentId("0");
treeNode.setNodeText(firstLevel[levelNum-1]);
treeNode.setOpenIcon("folderOpen.gif");
treeNode.setCloseIcon("folderClosed.gif");
treeNode.setLeafIcon("folderOpen.gif");
treeModel.addNode(levelNum+"", "0", treeNode);
}
}
catch (Exception e)
{
LOGGER.error(e, "##loadTreeModel()加载树形结构异常");
}
return treeModel;
}
<script type="text/javascript" src="${request.contextPath}/dwr/interface/TreeService.js"></script>
<script type="text/javascript" src="${request.contextPath}/dwr/interface/TreeNode.js"></script>
<script type="text/javascript" src="${request.contextPath}/js/common/UITree/js/UITree.js"></script>
<script src="${request.contextPath}/dwr/engine.js"></script>
<script src="${request.contextPath}/dwr/util.js"></script>
<body>
<div id="tree_favorite" align="left" />
<script type="text/javascript" defer>
var dyntree = new UITree("tree_favorite", "100%", "100%", false, false, false, "yourServiceNameHere", false, true, false);
dyntree.setImagePath("${request.contextPath}/js/common/UITree/imgs/");
dyntree.setLabelClickHandler(nodeClicked);
dyntree.setImageClickHandler(nodeClicked);
addItemsFromServer();
function addItemsFromServer()
{
Rule.displayRoot = false;
Rule.complete = true;
Rule.level = 2;
Rule.data = "${srTypeId}";
dyntree.loadTree(Rule);
}
function nodeClicked(nodeID)
{
var clickNode = dyntree.getNodeFromMap(nodeID);
var result = new Object();
result.id = nodeID;
result.name = clickNode.title;
result.value = clickNode.target;
if (clickNode.isLeaf())
{
window.returnValue = result;
window.close();
}
else
{
alert("请选择相应的叶子节点!");
}
}
</script>
</body>
</html>
function chooseProcessResp(keyName,showName)
{
var obj = new Date();
var action = "${request.contextPath}/yourActionNameHere.action?time=" + obj.getTime();
var spec = "scroll:yes;center:yes;resizable:yes;status:no;help:no;dialogWidth:300px;dialogHeight:400px";
var result = window.showModalDialog(action, '', spec);
if (result)
{
document.getElementById(keyName).value = result.name;
document.getElementById(showName).value = result.id;
}
};
[struts2 配置文件:]
<action id="yourActionNameHere">
<result id="success" type="jsp" path="/WEB-INF/treeShowDialogHere.jsp" />
<action>
[struts2 配置文件:]
<service id="yourServiceNameHere" class="com.XXXXX.yourServiceNameHereImpl">
<service>
[Struts2配置文件,请求某个action页面直接跳到相关的页面]
<action id="autoArchive">
<result id="success" type="jsp" path="yourPackageNameHere/leftTreeNameHere.jsp" />
<action>
<%@ page language="java" contentType="text/html;charset=UTF-8" session="false"%>
<frameset id="autoArchiveFrame" rows="*" cols="210,*" frameborder="no" scrolling="no">
<frame name="treeFrame" src="youTreeActionNameHere.action" scrolling="no"/>
<frame name="queryFrame" src="yourFristActionNameHere.action" scrolling="no"/>
</frameset>
function nodeClicked(nodeId)
{
var clickNode = dyntree.getNodeFromMap(nodeId);
if (clickNode.isLeaf())
{
var url = "";
parent.frames("queryFrame").location.href=encodeURI(url);
}
}
<script language="JavaScript" type="text/JavaScript">
// 初次加载所有数据
function init()
{
document.all.queryFrm.submit();
}
<body class="ballistic_bg" οnlοad="init()">
<form name="queryFrm" id="queryFrm" method="post" validate="true" target="resultPage" action="yourPackageNameHere/XXXXAction">
<iframe name="resultPage" width="100%" height="60%" frameborder="no" scrolling="no"></iframe>
</form>
</body>