二级联动菜单代码

二级联动菜单代码(AJAX+JAVA)

做项目经常要用到的,现在只给出一个不用数据库的代码,可作参考:
/**
*category.jsp 含有AJAX程序
*
*/
[java]
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
  <head>
    <title>二级菜单联动演示</title>
  <script type="text/javascript">
    var req;
    window.onload=function(){//页面加载时的函数

    }
   
    function Change_Select(){//当第一个下拉框的选项发生改变时调用该函数
      var zhi = document.getElementById('hero').value;
      var url = "servlet/select?id="+ escape(zhi);
      if(window.XMLHttpRequest){
        req = new XMLHttpRequest();
      }else if(window.ActiveXObject){
        req = new ActiveXObject("Microsoft.XMLHTTP");
      }
      
      if(req){
        req.open("GET",url,true);
        req.onreadystatechange = callback; //指定回调函数为callback
        req.send(null);
      }
    }
   
    function callback(){
      if(req.readyState ==4){
        if(req.status ==200){
          parseMessage();//解析XML文档
        }else{
          alert("Not able to retrieve description" + req.statusText);
        }
      }
    }
   
    function parseMessage(){
      var xmlDoc = req.responseXML.documentElement;//获得返回的XML文档
      var xSel = xmlDoc.getElementsByTagName('select');
      //获得XML文档中的所有<select>标记
      var select_root = document.getElementById('skill');
      //获得网页中的第二个下拉框
      select_root.options.length=0;
      //每次获得新的数据的时候先把每二个下拉框架的长度清0
      
      for(var i=0;i<xSel.length;i++){
        var xValue = xSel[i].childNodes[0].firstChild.nodeValue;
        //获得每个<select>标记中的第一个标记的值,也就是<value>标记的值
        var xText = xSel[i].childNodes[1].firstChild.nodeValue;
        //获得每个<select>标记中的第二个标记的值,也就是<text>标记的值
        
        var option = new Option(xText, xValue);
        //根据每组value和text标记的值创建一个option对象
        
        try{
          select_root.add(option);//将option对象添加到第二个下拉框中
        }catch(e){
        }
      }
    }        
  </script>
  </head>

  <body>
  <div align="center">
  <form name="form1" method="post" action="">
  <table width="70%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td align="center">Double Select Box</td>
    </tr>
    <tr>
    <td>
    <select name="hero" id="hero" onChange="Change_Select()">
    <!--第一个下拉菜单-->
      <option value="0">Unbounded</option>
      <option value="1">D.K. </option>
      <option value="2">NEC. </option>
      <option value="3">BOSS </option>
    </select>
    <select name="skill" id="skill">
    <!--第二个下拉菜单-->
      <option value="0">Unbounded</option>
    </select>
    </td>
    </tr>
    <tr>
    <td> </td>
    <tr>
    </table>
  </form>
  </div>
  </body>
</html>
[/java]

2007-3-27 15:00 bufegar
/**
*SelectServelet.java 处理页面返回的数据,返回XML给AJAX使用
*
*/

[java]
package onlinetest.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SelectServlet extends HttpServlet {

  /**
   * The doGet method of the servlet. <br>
   *
   * This method is called when a form has its tag value method equals to get.
   *
   * @param request
   *            the request send by the client to the server
   * @param response
   *            the response send by the server to the client
   * @throws ServletException
   *             if an error occurred
   * @throws IOException
   *             if an error occurred
   */
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/xml");
    response.setHeader("Cache-Control", "no-cache");
    String targetId = request.getParameter("id").toString();
    // 获得请求中参数为id的值
    String xml_start = "<selects>";
    String xml_end = "</selects>";
    String xml = "";

    if (targetId.equalsIgnoreCase("0")) {
      xml = "<select><value>0</value><text>Unbounded</text></select>";
    } else if (targetId.equalsIgnoreCase("1")) {
      xml = "<select><value>1</value><text>Mana Burn</text></select>";
      xml += "<select><value>2</value><text>Death Coil</text></select>";
      xml += "<select><value>3</value><text>Unhkly Aura</text></select>";
      xml += "<select><value>4</value><text>Unholy Fire</text></select>";
    } else if (targetId.equalsIgnoreCase("2")) {
      xml = "<select><value>1</value><text>Corpexplode</text></select>";
      xml += "<select><value>2</value><text>Raise Dead</text></select>";
      xml += "<select><value>3</value><text>Brilliance Aura</text></select>";
      xml += "<select><value>4</value><text>Aim Aura</text></select>";
    } else {// 如果是3,则返回下面的字符
      xml = "<select><value>1</value><text>Rain of Chaos</text></select>";
      xml += "<select><value>2</value><text>Finger of Death</text></select>";
      xml += "<select><value>3</value><text>Bash</text></select>";
      xml += "<select><value>4</value><text>Summon Doom</text></select>";
    }

    String last_xml = xml_start + xml + xml_end;
    response.getWriter().write(last_xml);

  }

  /**
   * The doPost method of the servlet. <br>
   *
   * This method is called when a form has its tag value method equals to
   * post.
   *
   * @param request
   *            the request send by the client to the server
   * @param response
   *            the response send by the server to the client
   * @throws ServletException
   *             if an error occurred
   * @throws IOException
   *             if an error occurred
   */
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    doGet(request, response);// 对于post的请求方式和get请求方式一样处理
  }

}
[/java]

2007-3-27 15:02 bufegar
/**
*web.xml 配置Servlet,可以使用MyEclipse创建Servlet,自动生成web.xml
*
*/

[xml]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="<a href="http://java.sun.com/xml/ns/j2ee"" target="_blank">[url]http://java.sun.com/xml/ns/j2ee[/url]"</a> xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance"" target="_blank">[url]http://www.w3.org/2001/XMLSchema-instance[/url]"</a> version="2.4" xsi:schemaLocation="<a href="http://java.sun.com/xml/ns/j2ee" target="_blank">[url]http://java.sun.com/xml/ns/j2ee[/url]</a>   <a href="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">" target="_blank">[url]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]"></a>
  <servlet>
    <servlet-name>select</servlet-name>
    <servlet-class>onlinetest.servlet.SelectServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>select</servlet-name>
    <url-pattern>/servlet/select</url-pattern>
  </servlet-mapping>
  
  <filter>
        <filter-name>characterFilter</filter-name>
        <filter-class>filters.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
   <filter-mapping>
    <filter-name>characterFilter</filter-name>
    <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>[/xml]

原文地址:[url]http://my.boolean.cn/read.php?130&part=3[/url]

posted on 2007-08-14 14:32 刘铮 阅读(1001) 评论(0)  编辑  收藏 所属分类: Ajax


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


网站导航:
 
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

留言簿(1)

文章分类(141)

文章档案(147)

搜索

最新评论