show.jsp <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030" import="java.util.*"%> <%@ page import="ambow.ecport.biz.dao.impl.*"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> <% @SuppressWarnings("unchecked") List list = new ArrayList(); CountryDaoBiz countryDao = new CountryDaoBiz(); list = countryDao.queryAll(); request.setAttribute("list", list); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> <center> <% int temp = 0; %> <Script language="javascript"> var onecount; onecount=0; subcat = new Array(); <c:forEach items="${list}" var="country"> <c:forEach items="${country.provinces}" var="province"> subcat[<%=temp%>] = new Array("${province.name}","${province.provinceid}","${country.countryid}"); <%temp++;%> </c:forEach> </c:forEach> onecount=<%=temp%>; provincelength = 29; function changelocation(locationid) { document.questionForm.provinceid.length = 0; var locationid=locationid; var i; for (i=0;i < onecount; i++) { if (subcat[i][2] == locationid) { document.questionForm.provinceid.options[document.questionForm.provinceid.length] = new Option(subcat[i][0], subcat[i][1]); } } } </script> <form method="post" name="questionForm"> <SELECT name="classId" onChange="changelocation(document.questionForm.classId.options[document.questionForm.classId.selectedIndex].value)" size="1"> <OPTION selected value> ==请选择== </OPTION> <c:forEach items="${list}" var="country"> <option value="${country.countryid}"> ${country.name} </option> </c:forEach> </select> <select name="provinceid"></select> </form> </center> </body> </html> 查出数据库中的数据 public List queryAll() { Session session = null; List list = null; Query query = null; try { session = hbn.getSession(); session.beginTransaction(); query = session.createQuery("FROM Country"); list = query.list(); session.getTransaction().commit(); } catch(Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } finally { hbn.closeSession(session); } return list; } 国家和省的对应关系 Country.hbm.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping package="ambow.ecport.entity"> <class name="Country" table="e_country"> <id name="countryid"> <generator class="native" /> </id> <property name="name" /> <property name="countryCode" /> <set name="provinces" table="e_Province" cascade="all" lazy="false" order-by="provinceCode"> <key> <column name="countryid"/> </key> <one-to-many class="Province"/> </set> </class> </hibernate-mapping> Product.hbm.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping package="ambow.ecport.entity"> <class name="Product" table="e_product"> <id name="productid"> <generator class="native" /> </id> <many-to-one name="categorys" class="Category"> <column name="categoryid"/> </many-to-one> <property name="name" /> <property name="description" /> <property name="basePrice" /> <property name="author" /> <property name="publish" /> <property name="pages" /> <property name="images" /> </class> </hibernate-mapping> 实体,可以按照xml文件来创建