📄 tree_show.jsp
字号:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="com.socialite.bizlogic.util.CookieTool,
com.socialite.bizlogic.util.Char,
com.socialite.bizlogic.client.BizLogicClient,
com.socialite.bizlogic.util.XMLObject,
com.socialite.bizlogic.tree.TreeNodeObject,
com.socialite.bizlogic.tree.TreeObject,
com.socialite.bizlogic.tree.TreeIterator,
javax.swing.tree.DefaultTreeModel,
javax.swing.tree.DefaultMutableTreeNode"%>
<%!
//把树结构组装成xml,以便用javascript显示
public String xmlTreeGet(TreeIterator it){
int curLevel=0; //当前树的层次
TreeNodeObject node=null;
DefaultMutableTreeNode dfNode=null;
String name=null;
String url=null;
String node_id=null;
StringBuffer treeXML=new StringBuffer("");
//遍历树,组装xml
if(it.hasNext()){
dfNode=(DefaultMutableTreeNode)it.nextNode();
node=(TreeNodeObject)dfNode.getUserObject();
name=node.getName();
url=node.getUrl();
node_id=node.getNodeId();
//根节点
treeXML.append("<DSTreeRoot text=\"" + name + "\" open=\"true\" href=\""+url+"\" target=\"box\" treeId=\""+node_id+"\" >");
}else{
return null;//树根都没有。
}
while(it.hasNext()){
dfNode=(DefaultMutableTreeNode)it.nextNode();
node=(TreeNodeObject)dfNode.getUserObject();
name=node.getName();
url=node.getUrl();
if(dfNode.isLeaf()){//是叶子节点
treeXML.append("<DSTree text=\"" + name + "\" href=\""+url+"\" target=\"box\" treeId=\""+node_id+"\" />");
xml.append(Char.LINE_SEPARATOR);
}else{//非叶子节点或根节点
treeXML.append("<DSTree text=\"" + name + "\" open=\"false\" href=\""+url+"\" target=\"box\" treeId=\""+node_id+"\" >");
}
if(dfNode.getLevel()<curLevel){//如果相等,说明前一个结点是叶子结点;如果大于,说明前一个结点是当前结点的父亲
for(int i=curLevel;i>dfNode.getLevel();i--)
treeXML.append("</DSTree>");
}
curLevel=dfNode.getLevel();
}
treeXML.append("</DSTreeRoot>");
return treeXML.toString();
}
%>
<%
// 如果操作员没有登录,则转到登录页面
String op_id = com.socialite.bizlogic.util.CookieTool.getCookieValue(request.getCookies(),"op_id");
if(op_id==null)
{
response.sendRedirect("/login/logout.jsp");
return;
} else if (op_id.equalsIgnoreCase("-1") ){
response.sendRedirect("/login/logout.jsp");
return;
}
request.setCharacterEncoding("gb2312");//处理中文显示问题
TreeObject tObject=null;
tObject=(TreeObject)application.getAttribute("tree");
if(tObject!=null){//application对象中已经保存树对象
}else{//application还没有保存树对象
//向bizlogic请求整棵树树信息
StringBuffer xml=new StringBuffer("<bizlogic><logic_name>com.socialite.bizlogic.businesslogic.treemanagementlogic.TreeManagementBusinessLogic</logic_name>");
xml.append(Char.LINE_SEPARATOR);
xml.append("<requesttype>SHOW_TREE</requesttype>");
xml.append(Char.LINE_SEPARATOR);
xml.append("<requestparameter>");
xml.append("</requestparameter>");
xml.append(Char.LINE_SEPARATOR);
xml.append("</bizlogic>");
xml.append(Char.LINE_SEPARATOR);
BizLogicClient hc=new BizLogicClient();
String xmlResult=hc.send(xml.toString());
XMLObject xmlObj = new XMLObject(xmlResult,0);
String result=xmlObj.getValue("bizlogic.resultcode");
if("0".equals(result)){
//构造TreeObject对象
tObject=new TreeObject(xmlObj);
//将tObject保存到application
application.setAttribute("tree",tObject);
}else{
return;
}
}
DefaultTreeModel dtm=tObject.dfModel;
TreeIterator it=new TreeIterator(dtm);//得到树的迭代器
String xml=xmlTreeGet(it);//用xml表示的树型结构,用于显示
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>动态树型结构管理</title>
<script language="javascript" >
//code by star time
var HC = "color:#990000;border:1px solid #cccccc";
var SC = "background-color:#efefef;border:1px solid #cccccc;color:#000000;";
var IO = null;
function initTree(){
var rootn = document.all.menuXML.documentElement;
var sd = 0;
document.onselectstart = function(){return false;}
document.all.treeBox.appendChild(createTree(rootn,sd));
}
function createTree(thisn,sd){
var nodeObj = document.createElement("span");
var upobj = document.createElement("span");
with(upobj){
style.marginLeft = sd*10;
className = thisn.hasChildNodes()?"hasItems":"Items";
innerHTML = "<img src=open.gif class=ec>" + thisn.getAttribute("text") +"";
onmousedown = function(){
if(event.button != 1) return;
if(this.getAttribute("cn")){
this.setAttribute("open",!this.getAttribute("open"));
this.cn.style.display = this.getAttribute("open")?"inline":"none";
this.all.tags("img")[0].src = this.getAttribute("open")?"open.gif":"contract.gif";
}
if(IO){
IO.runtimeStyle.cssText = "";
IO.setAttribute("selected",false);
}
IO = this;
this.setAttribute("selected",true);
this.runtimeStyle.cssText = SC;
}
onmouseover = function(){
if(this.getAttribute("selected"))return;
this.runtimeStyle.cssText = HC;
}
onmouseout = function(){
if(this.getAttribute("selected"))return;
this.runtimeStyle.cssText = "";
}
oncontextmenu = contextMenuHandle;
onclick = clickHandle;
}
if(thisn.getAttribute("treeId") != null){
upobj.setAttribute("treeId",thisn.getAttribute("treeId"));
}
if(thisn.getAttribute("href") != null){
upobj.setAttribute("href",thisn.getAttribute("href"));
}
if(thisn.getAttribute("target") != null){
upobj.setAttribute("target",thisn.getAttribute("target"));
}
nodeObj.appendChild(upobj);
nodeObj.insertAdjacentHTML("beforeEnd","<br>")
if(thisn.hasChildNodes()){
var i;
var nodes = thisn.childNodes;
var cn = document.createElement("span");
upobj.setAttribute("cn",cn);
if(thisn.getAttribute("open") != null){
upobj.setAttribute("open",(thisn.getAttribute("open")=="true"));
upobj.getAttribute("cn").style.display = upobj.getAttribute("open")?"inline":"none";
if( !upobj.getAttribute("open"))upobj.all.tags("img")[0].src ="contract.gif";
}
for(i=0;i<nodes.length;cn.appendChild(createTree(nodes[i++],sd+1)));
nodeObj.appendChild(cn);
}
else{
upobj.all.tags("img")[0].src ="endnode.gif";
}
return nodeObj;
}
//下面的三个函数是我添加的
function addOnclicked(){
window.open('node_create.jsp?node_id='+IO.getAttribute("treeId"),null,'height=270,width=530,status=no,toolbar=no,menubar=no,scrollbars=yes');
return false;
}
function modifyOnclicked(){
window.open('node_modify.jsp?node_id='+IO.getAttribute("treeId"),null,'height=270,width=530,status=no,toolbar=no,menubar=no,scrollbars=yes');
return false;
}
function deleteOnclicked(){
window.open('node_delete.jsp?node_id='+IO.getAttribute("treeId"),null,'height=270,width=530,status=no,toolbar=no,menubar=no,scrollbars=yes');
return false;
}
window.onload = initTree;
</script>
<script language="javascript">
function clickHandle(){
}
function contextMenuHandle()
{
event.returnValue = false;
var treeId = this.getAttribute("treeId");
}
</script>
</head>
<body>
<xml id=menuXML>
<?xml version="1.0" encoding="GB2312"?>
<%
out.println(xml);
%>
</xml>
<table width="803" height="375" border="0.2">
<tr>
<td height="33">树型结构管理</td>
</tr>
<tr>
<td id=treeBox valign=top> </td>
</tr>
<tr>
<td colspan="8" background="images/bg_table2.gif" height="7"></td>
</tr>
<tr>
<td height="35"><div align="center"><a onclick=addOnclicked()>增加子节点</a> <a onclick=modifyOnclicked()>修改节点</a> <a onclick=deleteOnclicked()>删除节点</a></div></td>
</tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -