網上看到一些例子,對於一個簡單的三級聯動,都加上什麼Struts, Hibernate諸如此類的架構。這個Ajax聯動殊不知和這些架構有什麼關係,一個小Demo幹嘛整得那麼大。
今天我做了一個dwr+jsp做的例子。
web.xml:
複製代碼 代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>
</servlet>
<servlet>
<servlet-name>SelectServlet</servlet-name>
<servlet-class>com.action.SelectServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SelectServlet</servlet-name>
<url-pattern>/select</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
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">
<dwr>
<!-- 沒有它DWR什麼也做不了 -->
<allow>
<create creator="new" javascript="menu">
<param name="class" value="com.dao.CountryDAO" />
</create>
<!-- 要轉換的Bean -->
<convert converter="bean" match="com.vo.Country" />
<convert converter="bean" match="com.vo.Province" />
<convert converter="bean" match="com.vo.City" />
</allow>
</dwr>
test.jsp:
複製代碼 代碼如下:
<%@ page language="java" import="java.util.*,com.vo.*"
pageEncoding="GBK"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<html>
<head>
<title>DWR三級聯動</title>
<script type='text/javascript'
src='/mutiplyMenu/dwr/interface/menu.js'></script>
<script type='text/javascript' src='/mutiplyMenu/dwr/engine.js'></script>
<script type='text/javascript' src='/mutiplyMenu/dwr/util.js'></script>
</head>
<body>
<script type="text/javascript">
//根據國家id查詢所屬省或州
function queryProvince()
{
var countryId = $("country").value;
//預設為不選擇
if(countryId == 0)
{
${"province"}.options.length=0;
${"city"}.options.length=0;
}
else
{
menu.queryProvinceById(countryId,provinceCallback);
}
}
//根據國家id查詢所屬省或州的回呼函數
function provinceCallback(provinces)
{
${"province"}.options.length=0;
//每次獲得新的資料的時候先把每二個下拉架構的長度清0
for(var i=0;i< provinces.length;i ++){
var value = provinces[i].id;
var text = provinces[i].provinceName;
var option = new Option(text, value);
//根據每組value和text標記的值建立一個option對象
try{
$("province").add(option);//將option對象添加到第二個下拉框中
}catch(e){
}
}
//同時關聯第三級
var provinceId = ${"province"}.value;
menu.queryCityById(provinceId,cityCallback);
}
//查詢所屬城市
function queryCity()
{
var provinceId = $("province").value;
menu.queryCityById(provinceId,cityCallback);
}
//查詢所屬城市回呼函數
function cityCallback(citys)
{
//每次獲得新的資料的時候先把每三個下拉架構的長度清0
${"city"}.options.length=0;
for(var i=0;i< citys.length;i ++){
var value = citys[i].id;
var text = citys[i].cityName;
var option = new Option(text, value);
//根據每組value和text標記的值建立一個option對象
try{
$("city").add(option);//將option對象添加到第三個下拉框中
}catch(e){
}
}
}
function change1()
{
queryProvince();
}
function change2()
{
queryCity();
}
</script>
<div align="center">
<h3>
<br>
</h3>
<h3>
DWR三級聯動示範
</h3>
<!-- 我都奇怪了,我的<c>標籤在這裡不能用 -->
<select id="country" onchange="change1();">
<option selected="selected" value="0">
請選擇
</option>
<%
@SuppressWarnings("unchecked")
List list = (List) request.getAttribute("countrys");
for (int i = 0; i < list.size(); i++)
{
Country temp = (Country) list.get(i);
%>
<option value="<%=temp.getId()%>"><%=temp.getCountryName()%></option>
<%
}
%>
</select>
<select id="province" onchange="change2();">
</select>
<select id="city">
</select>
</div>
</body>
</html>
servlet,dao就不帖了,想瞭解的可以去下載源碼