随笔 - 6  文章 - 129  trackbacks - 0
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(14)

随笔档案(6)

文章分类(467)

文章档案(423)

相册

收藏夹(18)

JAVA

搜索

  •  

积分与排名

  • 积分 - 821471
  • 排名 - 49

最新评论

阅读排行榜

评论排行榜

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

只有注册用户登录后才能发表评论。


网站导航: