JAVA网页设计不必迭代算法而疾速完成的jsp树布局
首先java功能强大的背后是其复杂性,就拿web来说,当今流行的框架有很多,什么struts,spring,jQuery等等,而这无疑增加了java的复杂性。js|算法在web页面上完成树状布局,有点贫苦.在比来的一个MIS体系的开辟中,我们项目组大批用到了树布局:好比职员的选择,单元的选择守候.
这个MIS体系所用的数据库是oracle9i.oracle9i的sql撑持迭代查询.我们的树是由牛人彭越写的,不外
也参照了收集上对照出名的xtree(能够到此下载:http://webfx.eae.net/),他的树算法撑持无穷级的树布局,不外功能仿佛
很慢.我持保存立场.
他用到的关头手艺就是这句话:
Stringsql="selectdwxh,dwbh,dwmc,dwfxh,levelccfromxt_dwconnectbypriordwxh=dwfxhstartwithdwfxh=0";
但是很多数据库不撑持迭代查询,而且迭代查询速率真是不克不及忍耐.有甚么更好的举措呢.上面说说我的办理计划.
一:需求的提出
1:客户必要一个关于部门职员的树布局,数据库为mysql4.1
2:java完成
二:建表:
1:
用户信息表:
各字段为:用户序号,用户编号,用户称号,单元序号,暗码,用户上岸号
createtableXT_YH
(
YHXHINT(9)NOTNULLauto_incrementPRIMARYKEY,
YHBHVARCHAR(30),
YHMCVARCHAR(30),
DWXHINT(9),
PWDVARCHAR(20),
YHDLHVARCHAR(30)
)
--拔出三条测试数据:
--insertintoxt_yh(yhbh,yhmc,dwxh,pwd,yhdlh)values(licl,李春雷,2,password,licl)
--insertintoxt_yh(yhbh,yhmc,dwxh,pwd,yhdlh)values(fengx,冯欣,2,password,fengx)
--insertintoxt_yh(yhbh,yhmc,dwxh,pwd,yhdlh)values(wangqx,王庆喷鼻,6,password,wangqx)
2:
单元部门表
各字段为:单元序号,单元编号,单元称号,单元父序号
createtableXT_DW
(
DWXHint(9)NOTNULLauto_incrementPRIMARYKEY,
DWBHVARCHAR(10),
DWMCVARCHAR(30),
DWFXHint(9)
)
--拔出5条测试数据
--insertintoxt_dw(dwbh,dwmc,dwfxh)values(0100000000,武汉科技局,0);
--insertintoxt_dw(dwbh,dwmc,dwfxh)values(0101000000,人事处,1);
--insertintoxt_dw(dwbh,dwmc,dwfxh)values(0102000000,后勤处,1);
--insertintoxt_dw(dwbh,dwmc,dwfxh)values(0101010000,人事处son1,2);
--insertintoxt_dw(dwbh,dwmc,dwfxh)values(0101020000,人事处son2,2);
--insertintoxt_dw(dwbh,dwmc,dwfxh)values(0102010000,后勤处son1,3);
注重:
为了完成疾速的树布局完成,我必要充实使用单元编号DWBH,DWBH才有10位编码,个中,第一第二位暗示一级单元,第三第四位暗示二级单元,
第五六位暗示三级单元...那末10位编码就能够完成五级单元的树布局.
好比:测试数据的树布局以下:
1武汉科技局:
2人事处
3人事处son1
3人事处son2
2后勤处
3后勤处son1
实在XT_DW表中的父序号是过剩的.不外假如你要用迭代算法来完成,就是必需的
才有10位编码,我只必要一句复杂疾速的sql语句就能够完成树布局:
Stringsql="selectdwxh,dwbh,dwmc,dwfxhfromxt_dworderbydwbh"
这句sql在几近一切的数据库平台都能实行,速率也快.
上面贴出接纳xtree,用10位编码而不是迭代算法完成的树:
/*******Constants.java**********/
packagecom.lcl.common;
publicclassConstants{
publicstaticfinalStringDBDRIVER="com.mysql.jdbc.Driver";//MYSQL驱动
publicstaticfinalStringDBUrl="jdbc:mysql://localhost/beauoa";//数据库url
publicstaticfinalStringUSERNAME="root";//数据库用户名
publicstaticfinalStringPASSWORD="root";//数据库暗码
}
/**********DbAccess.java****************/
packagecom.lcl.common;
importjava.sql.*;
importjava.lang.*;
/**
*@author李春雷
*
*TODO要变动今生成的范例正文的模板,请转至
*数据库会见类
*/
publicclassDbAccess
{
StringstrDBDriver=Constants.DBDRIVER;
StringstrDBUrl=Constants.DBUrl;
Stringusername=Constants.USERNAME;
Stringpassword=Constants.PASSWORD;
privateConnectionconn=null;
privateStatementstmt=null;
ResultSetrs=null;
//注册数据库驱动程序
publicDbAccess()
{
try
{
Class.forName(strDBDriver);
}
//非常处置
catch(java.lang.ClassNotFoundExceptione)
{
System.err.println("DbAccess():"+e.getMessage());
}
}
//创建数据库毗连及界说数据查询
publicResultSetexecuteQuery(Stringsql)
{
rs=null;
try
{
conn=DriverManager.getConnection(strDBUrl,username,password);
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLExceptionex)
{
System.err.println("ap.executeQuery:"+ex.getMessage());
}
returnrs;
}
//界说数据操库作
publicvoidexecuteUpdate(Stringsql)
{
stmt=null;
rs=null;
try
{
conn=DriverManager.getConnection(strDBUrl,username,password);
stmt=conn.createStatement();
stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLExceptionex)
{
System.err.println("ap.executeQuery:"+ex.getMessage());
}
}
//封闭数据库
publicvoidcloseStmt()
{
try
{
stmt.close();
}
catch(SQLExceptione)
{
e.printStackTrace();
}
}
publicvoidcloseConn()
{
try
{
conn.close();
}
catch(SQLExceptione)
{
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
System.out.println("hello,itstest");
DbAccessdbaccess=newDbAccess();
Stringsql="select*fromxt_yh";
ResultSetrs=dbaccess.executeQuery(sql);
try
{
while(rs.next()){
System.out.print(rs.getString(1)+rs.getString(2)+rs.getString(3)+rs.getString(4)+rs.getString(5)+rs.getString(6));
System.out.println();
}
dbaccess.closeStmt();
dbaccess.closeConn();
}
catch(SQLExceptione)
{
//TODO主动天生catch块
e.printStackTrace();
}
}
}
/*********DepEmplConfig.jsp************/
<%@pagecontentType="text/html;charset=gb2312"language="java"import="java.sql.*,com.lcl.common.*"errorPage=""%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
<title>无题目文档</title>
<HEAD>
<scripttype="text/javascript"src="../resources/xDataTree.js"></script>
<linktype="text/css"rel="stylesheet"href="../resources/xtree.css"/>
<styletype="text/css">
body{
background:white;
color:black;
}
</style>
<TITLE>NewDocument</TITLE>
<METANAME="Generator"CONTENT="EditPlus">
<METANAME="Author"CONTENT="">
<METANAME="Keywords"CONTENT="">
<METANAME="Description"CONTENT="">
</HEAD>
<scripttype="text/javascript">
webFXTreeConfig.rootIcon="../resources/images/xp/folder.png";
webFXTreeConfig.openRootIcon="../resources/images/xp/openfolder.png";
webFXTreeConfig.folderIcon="../resources/images/xp/folder.png";
webFXTreeConfig.openFolderIcon="../resources/images/xp/openfolder.png";
webFXTreeConfig.fileIcon="../resources/images/xp/file.png";
webFXTreeConfig.lMinusIcon="../resources/images/xp/Lminus.png";
webFXTreeConfig.lPlusIcon="../resources/images/xp/Lplus.png";
webFXTreeConfig.tMinusIcon="../resources/images/xp/Tminus.png";
webFXTreeConfig.tPlusIcon="../resources/images/xp/Tplus.png";
webFXTreeConfig.iIcon="../resources/images/xp/I.png";
webFXTreeConfig.lIcon="../resources/images/xp/L.png";
webFXTreeConfig.tIcon="../resources/images/xp/T.png";
webFXTreeConfig.blankIcon="../resources/images/blank.png";
vartree=newWebFXTree("单元职员基础情形","R0");
varchild;
varnodeToAddPerson;
functionaddDeptTreeNode(preNodeLevel,curNodeLevel,dispLabel,sKey,sTag){
if(curNodeLevel==1){
child=tree.add(newWebFXTreeItem(dispLabel,sKey,sTag));
}
else{
if(curNodeLevel==preNodeLevel){
if(child.parentNode)
child=child.parentNode.add(newWebFXTreeItem(dispLabel,sKey,sTag));
}
if(curNodeLevel>preNodeLevel){
child=child.add(newWebFXTreeItem(dispLabel,sKey,sTag));
}
if(curNodeLevel<preNodeLevel){
for(i=0;i<preNodeLevel-curNodeLevel+1;i++)
child=child.parentNode;
child=child.add(newWebFXTreeItem(dispLabel,sKey,sTag));
}
}
returnchild;
}
functiontreeClick(){
if(tree.getSelected()){
if(tree.getSelected().childNodes.length==0&&tree.getSelected().key!="R0")
cmdDelete.disabled=false;
else
cmdDelete.disabled=true;
if(tree.getSelected().key.substr(0,2)=="RZ"){
cmdAddDept.disabled=true;
cmdAddPeople.disabled=true;
varstrYhxh;
strYhxh=tree.getSelected().key.substr(2);
//window.open("../userAdm/editYh.do?yhxh="+strYhxh,"main");
}
elseif(tree.getSelected().key.substr(0,2)=="RB"){
cmdAddDept.disabled=false;
cmdAddPeople.disabled=false;
varstrDwxh;
strDwxh=tree.getSelected().key.substr(2);
//window.open("../userAdm/editBm.do?dwxh="+strDwxh,"main");
}
else{
cmdAddDept.disabled=false;
cmdAddPeople.disabled=true;
//window.open("yhroot.jsp","main");
}
}
}
functionaddPeople(){
varstrDwxh;
if(tree.getSelected()){
if(tree.getSelected().key.substr(0,2)=="RB"){
strDwxh=tree.getSelected().key.substr(2);
//window.open("../userAdm/addYh.do?dwxh="+strDwxh,"main");
alert("addPeople");
}
}
}
functionaddDept(){
varstrDwxh;
if(tree.getSelected()){
if(tree.getSelected().key.substr(0,2)=="RB"){
strDwfxh=tree.getSelected().key.substr(2);
//window.open("../userAdm/addBm.do?dwfxh="+strDwfxh,"main");
alert("addDept");
}
elseif(tree.getSelected().key=="R0"){
//window.open("../userAdm/addBm.do?dwfxh=0","main");
alert("addDept");
}
}
}
functiondeleSelected(){
if(!confirm("确认删除该节点吗?"))
return;
if(tree.getSelected()){
if(tree.getSelected().key.substr(0,2)=="RB"){
varstrDwxh;
strDwxh=tree.getSelected().key.substr(2);
//window.open("../userAdm/delBm.do?dwxh="+strDwxh,"main");
alert("deleSelected");
}
elseif(tree.getSelected().key.substr(0,2)==RZ){
varstrYhxh,strYhbh;
strYhxh=tree.getSelected().key.substr(2);
strYhbh=tree.getSelected().tag;
//window.open("../userAdm/delYh.do?yhxh="+strYhxh+"&yhbh="+strYhbh,"main");
alert("deleSelected");
}
}
}
functionremoveNode(){
if(tree.getSelected()){
varnode=tree.getSelected();
node.remove();
}
}
functionaddPeopleNode(strParentKey,strKey,strText,strTag){
if(tree.getSelected()){
varnode=tree.getSelected();
varchildNode;
//node.expand();
childNode=node.add(newWebFXTreeItem(strText,strKey,strTag,"","","../resources/images/people1.png"));
node.expand();//whyIdoso?Idontwanttotellyou,hah!
childNode.focus();
treeClick();
}
}
functionaddDeptNode(strParentKey,strKey,strText,strTag){
if(tree.getSelected()){
varnode=tree.getSelected();
varchildNode;
childNode=node.add(newWebFXTreeItem(strText,strKey,strTag));
node.expand();
childNode.focus();
treeClick();
}
}
functionupdateDeptNode(strTag,strText){
if(tree.getSelected()){
varnode=tree.getSelected();
node.text=strText;
node.tag=strTag;
node.focus();
}
}
functionupdatePeopleNode(strTag,strText){
if(tree.getSelected()){
varnode=tree.getSelected();
node.text=strText;
node.tag=strTag;
node.focus();
}
}
</script>
<%
intdwxh;
intdwfxh;
intyhxh;
Stringdwbh=null;
Stringdwmc=null;
Stringyhmc=null;
Stringyhbh=null;
intpreLevel=1;
intlevel=1;
DbAccessdbaccess=newDbAccess();
Stringsql="selectdwxh,dwbh,dwmc,dwfxhfromxt_dworderbydwbh";
ResultSetrs=dbaccess.executeQuery(sql);
try
{
while(rs.next())
{
dwxh=rs.getInt(1);
dwbh=rs.getString(2);
dwmc=rs.getString(3);
dwfxh=rs.getInt(4);
//经由过程单元编号盘算level
Stringlast=dwbh.substring(9,10);
inti=9;
while(last.equals("0")&&i>0){
i--;
last=dwbh.substring(i,i+1);
}
if(i==0||i==1)level=1;
if(i==2||i==3)level=2;
if(i==4||i==5)level=3;
if(i==6||i==7)level=4;
if(i==8||i==9)level=5;
//
%>
<scripttype="text/javascript">
nodeToAddPerson=addDeptTreeNode(<%=preLevel%>,<%=level%>,"<%=dwmc%>","RB<%=dwxh%>","<%=dwbh%>");
</script>
<%
preLevel=level;
Stringsubsql="selectyhxh,yhmc,yhbhfromxt_yhwheredwxh="+Integer.toString(dwxh);
ResultSetsubRs=dbaccess.executeQuery(subsql);
while(subRs.next()){
yhxh=subRs.getInt(1);
yhmc=subRs.getString(2);
yhbh=subRs.getString(3);
%>
<scripttype="text/javascript">
nodeToAddPerson.add(newWebFXTreeItem("<%=yhmc%>","RZ<%=yhxh%>","<%=yhbh%>","","","../resources/images/people1.png"));
</script>
<%
}
}
dbaccess.closeStmt();
dbaccess.closeConn();
}
catch(Exceptione)
{
}
%>
<basetarget="_self">
<METAHTTP-EQUIV="PRAGMA"CONTENT="NO-CACHE">
</head>
<body>
<tableborder="0"width="100%"cellspacing="0"cellpadding="0">
<tr>
<tdwidth="273"colspan="2">
<fontface="宋体"size="3">
</font>
</td>
</tr>
<tr>
<thwidth="33%"align="center"nowrap>
<palign="center">
<INPUTid=cmdAddDeptname="AddDept"type=buttonvalue="增添部门"style="FONT-FAMILY:楷体_GB2312;FONT-SIZE:12pt;FONT-WEIGHT:bold;HEIGHT:24px;WIDTH:80px">
</p>
</th>
<thwidth="33%"align="center"nowrap>
<palign="center">
<INPUTid=cmdAddPeoplename="AddPeople"type=buttonvalue="增添用户"style="FONT-FAMILY:楷体_GB2312;FONT-SIZE:12pt;FONT-WEIGHT:bold;HEIGHT:24px;WIDTH:80px">
</p>
</th>
<thwidth="33%"align="center"nowrap>
<palign="center">
<INPUTid=cmdDeletename="Delete"type=buttonvalue="删除"style="FONT-FAMILY:楷体_GB2312;FONT-SIZE:12pt;FONT-WEIGHT:bold;HEIGHT:24px;WIDTH:80px"disabled>
</p>
</th>
</tr>
<tr>
<tdwidth="273"height="8"colspan="2">
</td>
</tr>
</table>
</body>
<div>
<scripttype="text/javascript">
document.write(tree);
</script>
</div>
</HTML>
//个中jsp页面上的几个javascript函数为同事牛人彭越所写,我没修改,在此申明.
为什么外国人还要写那些框架进行代码封装,他们不就是为了别人使用时可以更简单么!如果要达到一个企业级项目的不用框架是很难的。小一些的项目还行,大的光是MVC模式的设计的编码量就够大的了。还有性能方面,单轮windows,这个工具是微软写的,。 一直感觉JAVA很大,很杂,找不到学习方向,前两天在网上找到了这篇文章,感觉不错,给没有方向的我指了一个方向,先不管对不对,做下来再说。 是一种简化的C++语言 是一种安全的语言,具有阻绝计算机病毒传输的功能 Java 不同于一般的编译执行计算机语言和解释执行计算机语言。它首先将源代码编译成二进制字节码(bytecode),然后依赖各种不同平台上的虚拟机来解释执行字节码。从而实现了“一次编译、到处执行”的跨平台特性。 是一种使网页(Web Page)由静态(Static)转变为动态(Dynamic)的语言 如果要向java web方向发展也要吧看看《Java web从入门到精通》学完再到《Struts2.0入门到精通》这样你差不多就把代码给学完了。有兴趣可以看一些设计模块和框架的包等等。 不过,每次的执行编译后的字节码需要消耗一定的时间,这同时也在一定程度上降低了 Java 程序的运行效率。 还好,SUN提供了Javabean可以把你的JSP中的 Java代码封装起来,便于调用也便于重用。 J2SE开发桌面应用软件比起 VC,VB,DEPHI这些传统开发语言来说,优势好象并不明显。J2ME对于初学者来说,好象又有点深奥,而且一般开发者很难有开发环境。
页:
[1]