JSP页面(address.jsp ):
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://ajaxtags.org/tags/ajax" prefix="ajax" %>
...
<script type="text/javascript" src="../js/prototype-1.4.0.js"></script>
<script type="text/javascript" src="../js/scriptaculous.js"></script>
<script type="text/javascript" src="../js/overlibmws.js"></script>
<script type="text/javascript" src="../js/ajaxtags.js"></script>
....
<select id="country" name="address.country">
<option value="">选择一个国家</option>
<option value="中国">中国</option>
<select id="province" name="address.province">
<option value="">省份列表</option>
</select>
<select id="city" name="address.city">
<option value="">城市列表</option>
</select>
<ajax:select source="country"
target="province"
baseUrl="../area.do"
parameters="areaName={country}"/>
<ajax:select source="province"
target="city"
baseUrl="../area.do"
parameters="areaName={province}"/>
AreaServlet.java
package dgut.ke.servlet;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ajaxtags.helpers.AjaxXmlBuilder;
import org.ajaxtags.servlets.BaseAjaxServlet;
import org.springframework.web.context.support.WebApplicationContextUtils;
import dgut.ke.model.Area;
import dgut.ke.util.AreaUtil;
@SuppressWarnings("serial")
public class AreaServlet extends BaseAjaxServlet {
@SuppressWarnings("unchecked")
@Override
public String getXmlContent(HttpServletRequest request,
HttpServletResponse response) throws Exception {
request.setCharacterEncoding("UTF-8");
AreaUtil areaUtil = (AreaUtil) WebApplicationContextUtils
.getRequiredWebApplicationContext(getServletContext())
.getBean("areaUtil");
String areaName = request.getParameter("areaName");
//将areaName转换成areaCode
String parentAreaCode = areaUtil.findAreaCodeByAreaName(areaName);
if(parentAreaCode != null && ! parentAreaCode.equals("")) {
List<Area> list = areaUtil.findByParentAreaCode(parentAreaCode);
AjaxXmlBuilder builder = new AjaxXmlBuilder();
for(Area area : list) {
builder = builder.addItem(area.getAreaName(), area.getAreaName());
}
return builder.toString();
} else {
return null;
}
}
}
Servlet配置
<!-- 省份地址下拉列表 -->
<servlet>
<servlet-name>areaServlet</servlet-name>
<servlet-class>dgut.ke.servlet.AreaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>areaServlet</servlet-name>
<url-pattern>/area.do</url-pattern>
</servlet-mapping>
posted on 2008-01-17 14:03
Ke 阅读(2707)
评论(0) 编辑 收藏 所属分类:
ajax