jquery+dwr二級聯動
阿新 • • 發佈:2019-02-07
package com.dwr2; import java.util.ArrayList; import java.util.List; import java.sql.*; import net.sf.json.*; public class Citybean { private Connection ct=null; private PreparedStatement ps=null; private ResultSet rs=null; public List allprvinces(){ List list=new ArrayList(); try{ ct=Conn.getinstance(); ps=ct.prepareStatement("select * from prvinces"); rs=ps.executeQuery(); Prvinces p=null; while(rs.next()){ p=new Prvinces(); p.setId(rs.getInt(1)); p.setPname(rs.getString(2)); list.add(p); } }catch(Exception e){ e.printStackTrace(); }finally{ DBclose.close(ct, ps, rs); } return list; } public List allCity(){ List all=new ArrayList(); try{ ct=Conn.getinstance(); ps=ct.prepareStatement("select cityname from prvinces,city where prvinces.id=city.id and pname='四川'"); rs=ps.executeQuery(); City c=null; while(rs.next()){ c=new City(); c.setCityname(rs.getString(1)); all.add(c); } }catch(Exception e) {e.printStackTrace(); }finally{ DBclose.close(ct, ps, rs); }return all; } public List selectCity(String pname){ List li=new ArrayList(); try{ ct=Conn.getinstance(); ps=ct.prepareStatement("select cityname from prvinces,city where prvinces.id=city.id and pname=?"); ps.setString(1, pname); rs=ps.executeQuery(); City c=null; while(rs.next()){ c=new City(); c.setCityname(rs.getString(1)); li.add(c); } }catch(Exception e){ e.printStackTrace(); }finally{ DBclose.close(ct, ps, rs); }return li; } public String all(String pname){ List li=this.selectCity(pname); JSONArray ja=JSONArray.fromObject(li); return ja.toString(); } }
dwr.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd" > <!-- <!DOCTYPE dwr SYSTEM "dwr20.dtd" > --> <dwr> <allow> <create creator="new" javascript="test"> <param name="class" value="com.dwr2.Citybean"/> <include method="all"/> </create> </allow> </dwr>
頁面
<%@ page language="java" import="java.util.*,com.dwr2.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; ArrayList list=(ArrayList)request.getAttribute("list"); ArrayList all=(ArrayList)request.getAttribute("all"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type='text/javascript' src='/dwr/dwr/interface/test.js'></script> <script type='text/javascript' src='/dwr/dwr/engine.js'></script> <script type='text/javascript' src='/dwr/dwr/util.js'></script> <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script> <base href="<%=basePath%>"> <title>My JSP 'First.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <select name="pname" id="prvince" onchange="show();"> <%for(int i=0;i<list.size();i++){ Prvinces p=(Prvinces)list.get(i); %> <option><%=p.getPname() %></option> <%} %> </select> <select name="cityname" id="city"> <option>請選擇城市</option> <%for(int j=0;j<all.size();j++){ City c=(City)all.get(j); %> <option><%=c.getCityname() %></option> <%} %> </select> </body> <script type="text/javascript"> function show(){ var prvince=$("#prvince"); var city=$("#city"); city.empty(); var pname=document.all("pname").value; test.all(pname,call) } function call(data){ if(data){ var c=eval(data); for(var k=0;k<c.length;k++){ $("#city").append("<option>"+c[k].cityname+"</option>"); } } } </script> </html>