Browser
<% @ 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">
Three entity classes
Package cn. yo. entity; import java. util. hashSet; import java. util. set; public class Province implements java. io. serializable {private Integer id; private String province; private Set cities = new HashSet (0); // The get, set Method} package cn. yo. entity; import java. util. hashSet; import java. util. set; public class City implements java. io. serializable {private Integer id; private Province province; private String city; private Set towns = new HashSet (0); // The get, set Method} package cn is omitted. yo. entity; public class Town implements java. io. serializable {private Integer id; private City city City; private String town; // The get, set method is omitted}
Dao class
Package cn. yo. dao; import java. util. list; import org. hibernate. session; import cn. yo. entity. city; import cn. yo. entity. province; import cn. yo. entity. town; public class ProvinceDao {// query all provinces public List <Province> findProvince () {Session session = HibernateSessionFactory. getSession (); String hql = "from Province"; List <Province> province = session. createQuery (hql ). list (); if (province. size ()> 0) {return province;} else {return null ;}// query all public lists in the province Based on the province Id <City> findCityById (int id) {Session session = HibernateSessionFactory. getSession (); String hql = "from City c where c. province. id = '"+ id +"' "; List <City> city = session. createQuery (hql ). list (); if (city. size ()> 0) {return city;} else {return null ;}// query all the towns in the city according to the city Id public List <Town> findTownById (int id) {Session session = HibernateSessionFactory. getSession (); String hql = "from Town t where t. city = '"+ id +"' "; List <Town> town = session. createQuery (hql ). list (); if (town. size ()> 0) {return town;} else {return null ;}}}
Web. xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- DWR --> <servlet> <servlet-name>dwr_invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>allowScriptTagRemoting</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr_invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping></web-app>
Dwr. xml: There is no DWR without it. Put it in the same directory of web. xml.
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE dwr PUBLIC 3 "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" 4 "http://getahead.org/dwr/dwr20.dtd"> 5 6 <dwr> 7 <allow> 8 <create javascript="mydwr" creator="new"> 9 <param name="class" value="cn.yo.dao.ProvinceDao"/>10 </create>11 <convert match="cn.yo.entity.Province" converter="bean"/>12 <convert match="cn.yo.entity.City" converter="bean"/>13 <convert match="cn.yo.entity.Town" converter="bean"/>14 </allow>15 </dwr>
I hope all the code will be provided to beginners. I hope you can give suggestions if not well written. Thank you, younger brother.
For original works, please indicate the source for reprinting.