This is a web project
The first is the Web. xml file.
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/javaee <br/> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <br/> <servlet-Name> DWR-invoker </servlet-Name> <br/> <servlet-class> Org. directwebremoting. servlet. dwrservlet </servlet-class> <br/> <init-param> <br/> <param-Name> debug </param-Name> <br/> <param-Value> true </param-value> <br/> </init-param> <br/> </servlet> <br/> <servlet-mapping> <br/> <servlet-Name> DWR-invoker </servlet-Name> <br/> <URL-pattern>/DWR/* </url-pattern> <br/> </servlet- mapping> </P> <p> <welcome-file-List> <br/> <welcome-File> index. JSP </welcome-File> <br/> </welcome-file-List> <br/> </Web-app>
Then there is the DWR configuration file DWR. xml located under // WEB-INF //
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! Doctype DWR public "-// getahead limited // DTD direct Web remoting 3.0 //" <br/> "http://getahead.org/dwr/dwr30.dtd"> <br/> <DWR> <br/> <allow> <br/> <create creator = "new" javascript = "jscity"> <br/> <Param name = "class" value = "com. jadyer. dao. citydao "/> <br/> </create> <br/> <convert match =" com. jadyer. model. city "converter =" Bean "/> <br/> </allow> <br/> </DWR>
Then the index. jsp page is used to display the infinite provincial/municipal linkage effect.
<% @ Page Language = "Java" pageencoding = "UTF-8" %> </P> <p> <SCRIPT type = 'text/JavaScript 'src = "/dwr_city/DWR/ interface/jscity. JS "> </SCRIPT> <br/> <SCRIPT type = 'text/JavaScript 'src ="/dwr_city/DWR/engine. JS "> </SCRIPT> <br/> <SCRIPT type = 'text/JavaScript 'src ="/dwr_city/DWR/util. JS "> </SCRIPT> </P> <p> <SCRIPT type =" text/JavaScript "> <br/> var thetagid; <br/> function initcity (parentcityid, tagid) {<br/> var isdel Ete = false; <br/> thetagid = tagid; <br/> var Sons = document. getelementbyid (tagid ). childnodes; <br/> for (VAR I = (sons. length-1); I> = 0; I --) {<br/> // If (""! = Sons [I]. nodename. touppercase () {// The two are equivalent <br/> If ("Ul" = sons [I]. nodename. touppercase () {<br/> sons [I]. parentnode. removechild (sons [I]); <br/> isdelete = true; <br/>}< br/> If (isdelete) {<br/> return; <br/>}< br/> jscity. findbyparentid (parentcityid, callback); <br/>}< br/> function callback (citys) {<br/> var citys = eval (citys ); <br/> var parentmodule = document. getelementbyid (thetagid); <br/> var n Ewul = document. createelement ("Ul"); // create the <ul> tag <br/> for (VAR I = 0; I <Citys. length; I ++) {<br/> var newli = document. createelement ("Li"); // create the <li> label <br/> newli. id = "tag" + citys [I]. cityid; // assign a value to the ID of <li>, that is, specify the id value in <li id = "">. Its ID is dynamically changed <br/> newli. innerhtml = "<a href = '# 'onclick = 'initcity (" + citys [I]. cityid + ",/" tag "+ citys [I]. cityid + "/"); '> "+ citys [I]. cityname + "</a>"; <br/> newul. appendchild (n Ewli); <br/>}< br/> parentmodule. appendchild (newul); <br/>}< br/> </SCRIPT> </P> <p> <! -- <Br/> the initcity (parent ID, 'control id') method is automatically called when the page is loaded, used to obtain information about all provinces <br/> the first parameter specifies the parent ID .. The second parameter specifies the Control ID, that is, the control to which the city information is to be placed, the first time I put it in <div> <br/> --> <br/> <body onload = "initcity (0, 'initprovince ') "> <br/> <Div id =" initprovince "> </div> <br/> </body>
City. Java
Package COM. jadyer. model; </P> <p> Import Java. util. list; </P> <p> public class city {<br/> private integer parentid; <br/> private integer cityid; <br/> private string cityname; <br/> private list <city> areas; // stores the detailed information of all districts and counties in the city </P> <p>/* the setter and getter corresponding to the four attributes are omitted */</P> <p> Public City () {}</P> <p> Public City (integer cityid, string cityname) {<br/> This. cityid = cityid; <br/> This. cityname = cityname; <br/>}< br/>}
Then, the DAO class operated directly with the Oracle database
Package COM. jadyer. dao; </P> <p> Import Java. SQL. connection; <br/> Import Java. SQL. preparedstatement; <br/> Import Java. SQL. resultset; <br/> Import Java. SQL. sqlexception; <br/> Import Java. util. arraylist; <br/> Import Java. util. list; </P> <p> Import javax. naming. context; <br/> Import javax. naming. initialcontext; <br/> Import javax. naming. namingexception; <br/> Import javax. SQL. datasource; </P> <p> Import COM. jadyer. Model. city; </P> <p>/** <br/> * The code here should be reconstructed, but for convenience of demonstration, no reconstruction <br/> */<br/> public class citydao {<br/>/** <br/> * obtain all primary classes based on the parent id <br/> * @ see if 0 is passed, all the root nodes <br/> */<br/> public list <city> findbyparentid (INT parentid) {<br/> string SQL = "select cityid, cityname from city where parentid =? "; <Br/> List <city> citys = new arraylist <city> (); <br/> try {<br/> context = new initialcontext (); <br/> // For JDBC/oracleds connection pool configuration, see http://blog.csdn.net/jadyer/archive/2010/11/10/6001023.aspx <br/> datasource DS = (datasource) context. lookup ("Java:/COMP/ENV/jdbc/oracleds"); <br/> connection conn = Ds. getconnection (); <br/> preparedstatement pstmt = Conn. preparestatement (SQL); <br/> pstmt. setint (1, Parentid); <br/> resultset rs = pstmt.exe cutequery (); <br/> while (RS. next () {<br/> Citys. add (New City (RS. getint ("cityid"), RS. getstring ("cityname"); <br/>}< br/>} catch (namingexception e) {<br/> E. printstacktrace (); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/>}< br/> return citys; <br/>}</P> <p>/** <br/> * obtain detailed information about the city based on the ID. <br/> */<br/> Public City fingbycityid (INT cityid) {<br/> St Ring SQL = "select cityname from city where cityid =? "; <Br/> city City = NULL; <br/> try {<br/> context = new initialcontext (); <br/> datasource DS = (datasource) context. lookup ("Java:/COMP/ENV/jdbc/oracleds"); <br/> connection conn = Ds. getconnection (); <br/> preparedstatement pstmt = Conn. preparestatement (SQL); <br/> pstmt. setint (1, cityid); <br/> resultset rs = pstmt.exe cutequery (); <br/> while (RS. next () {<br/> city = New City (cityid, Rs. getstring ("cityname"); <br/>}< br/>} catch (namingexception e) {<br/> E. printstacktrace (); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/>}< br/> return city; <br/>}< br/>}
Finally, the database script file
-- Oracle 11g <br/> -- create a table <br/> Create Table City (<br/> ID number (3) primary key, <br/> parentid number (3), -- 0 indicates the root node <br/> cityid number (3), <br/> cityname varchar (20) <br/>) </P> <p> -- create sequence <br/> Create sequence sequence_city increment by 1 start with 1 nomaxvalue nocycle; </P> <p> -- add Level 1 menu <br/> insert into city values (sequence_city.nextval, 0, 1, 'heilongjiang '); <br/> insert into city values (sequence_city.nextval, 0, 2, 'Jilin province '); <br/> insert into city values (sequence_city.nextval, 0, 3, 'liaoning province '); <br/> -- add Level 2 menu <br/> insert into city values (sequence_city.nextval, 1, 4, 'harbin '); <br/> insert into city values (sequence_city.nextval, 1, 5, 'jiamusi '); <br/> insert into city values (sequence_city.nextval, 1, 6, 'dual yashan'); <br/> insert into city values (sequence_city.nextval, 2, 7, 'changchun '); <br/> insert into city values (sequence_city.ne Xtval, 2, 8, 'liaoyuan'); <br/> insert into city values (sequence_city.nextval, 2, 9, 'tonghua '); <br/> insert into city values (sequence_city.nextval, 3, 10, 'shenyang '); <br/> insert into city values (sequence_city.nextval, 3, 11, 'dalian '); <br/> insert into city values (sequence_city.nextval, 3, 12, 'tieling '); <br/> -- add a three-level menu <br/> -- only the three-level linkage data of Heilongjiang Province is provided. For data about other provinces, add <br/> insert into city values (sequence_city.nextval, 4, 13, 'bayan County '); <br/> insert into city values (sequence_city.nextval, 4, 14, 'yanshou County'); <br/> insert into city values (sequence_city.nextval, 4, 15, 'mulan statement'); <br/> insert into city values (sequence_city.nextval, 5, 16, 'fuyuan status'); <br/> insert into city values (sequence_city.nextval, 5, 17, 'tangyuan County '); <br/> insert into city values (sequence_city.nextval, 5, 18, 'huachuan County'); <br/> insert into city values (sequence_city.nextval, 6, 19,' Ji Xian '); <br/> insert into city values (sequence_city.nextval, 6, 20, 'babyqingxian'); <br/> insert into city values (sequence_city.nextval, 6, 21, 'youyouxian '); <br/> -- add Level 4 menu <br/> -- only the level 4 linkage data of Harbin is provided, and other data is provided, add <br/> insert into city values (sequence_city.nextval, 13, 22, 'xing longzhen ') by yourself; <br/> insert into city values (sequence_city.nextval, 13, 23, 'longmiao Zhen '); <br/> insert into city values (sequence_city.nextval, 13, 24, 'suxing Zhen'); <br/> Insert into city values (sequence_city.nextval, 14, 25, 'liutuzhen '); <br/> insert into city values (sequence_city.nextval, 14, 26, 'zhonghe town '); <br/> insert into city values (sequence_city.nextval, 14, 27, 'trust town '); <br/> insert into city values (sequence_city.nextval, 15, 28, 'Dong Xing Zhen '); <br/> insert into city values (sequence_city.nextval, 15, 29, 'Grand town '); <br/> insert into city values (sequence_city.nextval, 15, 30, 'new min Zhen' ); <Br/> -- add a menu of five levels <br/> -- only five levels of association data in Bayan County are provided, and other data are provided, add <br/> insert into city values (sequence_city.nextval, 22, 31, 'changchun township '); <br/> insert into city values (sequence_city.nextval, 22, 32, 'fenglexiang'); <br/> insert into city values (sequence_city.nextval, 22, 33, 'dianxiang xiang'); <br/> insert into city values (sequence_city.nextval, 23, 34, 'mountain output'); <br/> insert into city values (sequence_city.nextval, 23, 35, 'huashan output'); <br/> INS ERT into city values (sequence_city.nextval, 23, 36, 'hongguang township '); <br/> insert into city values (sequence_city.nextval, 24, 36, 'Dongxiang town '); <br/> insert into city values (sequence_city.nextval, 24, 36, 'fu Jiangxiang '); <br/> insert into city values (sequence_city.nextval, 24, 36, 'songhuaxiangxiang '); <br/> -- add level 6 menu <br/> -- theoretically unlimited. However, in practice, there are not many levels of menus used, so the following data is omitted: