DWR实现省市级联

前端之家收集整理的这篇文章主要介绍了DWR实现省市级联前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

项目部署为:

数据库sql代码为:

--省级 provincial

[sql] view plain copy print ? @H_404_11@ @H_404_11@
  1. create table provincial (
  2. provincialID int,
  3. provincialName varchar(50),
  4. primary key (provincialID)
  5. )engine=INNODB default charset=gb2312;
@H_404_11@


--城市 city

[sql] view plain copy print ? @H_404_11@ @H_404_11@
  1. create table city (
  2. cityID int not null,
  3. cityName varchar(50) not null,
  4. provincialID int not null,
  5. primary key (cityID)
  6. )engine=INNODB default charset=gb2312;
@H_404_11@

详细数据库代码如下链接

http://download.csdn.net/detail/aigochina/4649480

下面的代码用于与数据库实现映射:
City.java类代码:(城市)

[java] view plain copy print ? @H_404_11@ @H_404_11@
  1. public class City {
  2. private int cityID;
  3. public int getCityID() {
  4. return cityID;
  5. }
  6. public void setCityID(int cityID) {
  7. this.cityID = cityID;
  8. }
  9. public String getCityName() {
  10. return cityName;
  11. }
  12. public void setCityName(String cityName) {
  13. this.cityName = cityName;
  14. }
  15. public int getProvincialID() {
  16. return provincialID;
  17. }
  18. public void setProvincialID(int provincialID) {
  19. this.provincialID = provincialID;
  20. }
  21. private String cityName;
  22. private int provincialID;
  23. }
@H_404_11@


Province.java类代码:(省)

[java] view plain copy print ? @H_404_11@ @H_404_11@
  1. public class Province {
  2. private int provincialID;
  3. public int getProvincialID() {
  4. return provincialID;
  5. }
  6. public void setProvincialID(int provincialID) {
  7. this.provincialID = provincialID;
  8. }
  9. public String getProvincialName() {
  10. return provincialName;
  11. }
  12. public void setProvincialName(String provincialName) {
  13. this.provincialName = provincialName;
  14. }
  15. private String provincialName;
  16. }
@H_404_11@


CityDao.java类代码:(实现与数据库操作)

[java] view plain copy print ? @H_404_11@ @H_404_11@
  1. import java.sql.Connection;
  2. import java.sql.PreparedStatement;
  3. import java.sql.ResultSet;
  4. import java.sql.sqlException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. public class CityDao {
  8. Connection conn=null;
  9. PreparedStatement pre=null;
  10. ResultSet res;
  11. //根据省份ID来获得相对应的城市信息
  12. public List<City> getCity(int provincialID){
  13. List<City> cityList=new ArrayList<City>();
  14. String sql="SELECT * FROM city WHERE provincialID=?";
  15. conn=new Basedao().getConnection();
  16. try {
  17. pre=conn.prepareStatement(sql);
  18. pre.setInt(1,provincialID);
  19. res=pre.executeQuery();
  20. while(res.next()){
  21. City city=new City();
  22. city.setCityID(res.getInt("cityID"));
  23. city.setCityName(res.getString("cityName"));
  24. city.setProvincialID(res.getInt("provincialID"));
  25. cityList.add(city);
  26. }
  27. } catch (sqlException e) {
  28. e.printStackTrace();
  29. }
  30. return cityList;
  31. }
  32. //获得所有的省份列表
  33. public List<Province> getProvince(){
  34. List<Province> proList=new ArrayList<Province>();
  35. String sql="SELECT * FROM provincial";
  36. conn=new ConnectDB().getConnection();
  37. try {
  38. pre=conn.prepareStatement(sql);
  39. res=pre.executeQuery();
  40. while(res.next()){
  41. Province pro=new Province();
  42. pro.setProvincialID(res.getInt("provincialID"));
  43. pro.setProvincialName(res.getString("provincialName"));
  44. proList.add(pro);
  45. }
  46. } catch (sqlException e) {
  47. e.printStackTrace();
  48. }
  49. return proList;
  50. }
  51. }
@H_404_11@


ConnectDB.java代码(用于实现数据库连接)

[java] view plain copy print ? @H_404_11@ @H_404_11@
  1. public class ConnectDB {
  2. public Connection conn=null;
  3. private String url="jdbc:MysqL://localhost:3306/test";
  4. private String user="root";
  5. private String password="lovemu";
  6. public Connection getConnection(){
  7. try {
  8. Class.forName("com.MysqL.jdbc.Driver");
  9. conn=DriverManager.getConnection(url,user,password);
  10. } catch (ClassNotFoundException e) {
  11. e.printStackTrace();
  12. } catch (sqlException e) {
  13. e.printStackTrace();
  14. }
  15. return conn;
  16. }
  17. }
@H_404_11@


dwr.xml代码如下:

[html] view plain copy print ? @H_404_11@ @H_404_11@
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">
  3. <dwr>
  4. <allow>
  5. <create creator="new" javascript="province">
  6. <param name="class" value="dao.CityDao"></param>
  7. </create>
  8. <convert converter="bean" match="dao.Province"></convert>
  9. <convert converter="bean" match="dao.City"></convert>
  10. </allow>
  11. </dwr>
@H_404_11@

实现省市级联的页面代码

[html] view plain copy print ? @H_404_11@ @H_404_11@
  1. <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'index.jsp' starting page</title>
  11. <Meta http-equiv="pragma" content="no-cache">
  12. <Meta http-equiv="cache-control" content="no-cache">
  13. <Meta http-equiv="expires" content="0">
  14. <Meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <Meta http-equiv="description" content="This is my page">
  16. <script type='text/javascript' src='/DWR/dwr/engine.js'></script>
  17. <script type='text/javascript' src='/DWR/dwr/interface/province.js'></script>
  18. <script type='text/javascript' src='/DWR/dwr/util.js'></script>
  19. </head>
  20. <body onload="getPro();">
  21. 所在地:<select onchange="getCity();" id="provicial" name="province"></select><select id="city" name="city"></select>
  22. </body>
  23. <script type="text/javascript">
  24. //获得省份列表
  25. function getPro() {
  26. province.getProvince(callPro);
  27. }
  28. //城市返回函数
  29. function callPro(data) {
  30. var provical = document.getElementById("provicial");
  31. provical.innerHTML = "";//如果有值就清空
  32. provical.add(new Option('请选择',''));
  33. for ( var i = 0; i < data.length; i++) {//循环添加省份下拉列表
  34. provicial.add(new Option(data[i].provincialName,
  35. data[i].provincialID));
  36. }
  37. }
  38. //根据省份ID获取相对应的城市列表
  39. function getCity() {
  40. var city = document.getElementById("provicial").value;
  41. //获得省份下拉列表的值
  42. province.getCity(city,callCity);
  43. }
  44. function callCity(data) {
  45. var city = document.getElementById("city");//将城市下拉列表清空
  46. city.innerHTML = "";
  47. city.add(new Option('请选择',''));
  48. for ( var i = 0; i < data.length; i++) {//循环添加城市下拉列表
  49. city.add(new Option(data[i].cityName,data[i].cityID));
  50. }
  51. }
  52. </script>
  53. </html>
@H_404_11@

其中所引用的js代码

[javascript] view plain copy print ? @H_404_11@ @H_404_11@
  1. <script type='text/javascript' src='/DWR/dwr/engine.js'></script>
  2. <script type='text/javascript' src='/DWR/dwr/interface/province.js'></script>
@H_404_11@
[javascript] view plain copy print ? @H_404_11@ @H_404_11@
  1. <script type='text/javascript' src='/DWR/dwr/util.js'></script>
@H_404_11@

来自以下页面

运行后,在浏览器地址栏输入:http://localhost:8080/DWR/dwr

页面如下:

点击黄颜色部分,进入页面

上面的javascrip代码即为红色框内的内容

最后,运行项目,运行结果为:

以上内容转自网络:http://www.jb51.cc/article/p-xulcmrly-qz.html

猜你在找的Ajax相关文章