ajax+struts级联无需数据库,绝对可以看到效果的例子

jsp文件
<%@ page language="java" pageEncoding="UTF-8"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
	<html:base />

	<title>cascade.jsp</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>
   <script language="javascript"  type ="text/javascript"> 
        var  xmlHttp; 
       var tempSel;
        function  createXMLHttpRequest() { 
            if  (window.ActiveXObject) { 
                xmlHttp=new  ActiveXObject("Microsoft.XMLHTTP"); 
           } else   if (window.XMLHttpRequest) { 
                xmlHttp=new  XMLHttpRequest(); 
           } 
       } 
       
       // 返回信息处理,二级机构下拉列表返回结果 
       function  callback2() { 
          if(xmlHttp.readyState==4 ){ 
            if(xmlHttp.status==200 ){ 
                var  xmlDoc=xmlHttp.responseXML; 
               changeSel('2',xmlDoc); 
           } else { 
                 window.alert("您所请求的页面有异常。"); 
           } 
         }           
      } 
      
        function  selectChangeListener(orgLevel,orgId){ 
         createXMLHttpRequest(); 
         var  url="cascade.do?method=getOrganinfo_ajax&orgLevel="+orgLevel+"&orgId="+orgId; 
         xmlHttp.open("get",url,true); 
          if (orgLevel=='2'){
              tempSel=document.getElementById("secOrganIdSel"); 
              xmlHttp.onreadystatechange=callback2; 
         } 
         xmlHttp.send(null); 
      } 
      
       // 更新机构下拉列表 
       function  changeSel(orgLevel,xmlDoc){ 
            var  xsel=xmlDoc.documentElement.childNodes;
            	tempSel.options.length=0 ; 
                for (var i=0;i<xsel.length;i++) { 
                    var  xvalue=xsel[i].childNodes[0].firstChild.nodeValue; 
                    var  xtext=xsel[i].childNodes[1].firstChild.nodeValue; 
                    var  option=new  Option(xtext,xvalue); 
	                tempSel.add(option); 
	                } 
	   	 } 
   </script> 
<body>
	<form action="" name="form1">
		<select name="name" id="name"
			onchange=" selectChangeListener('2',this.options[this.selectedIndex].value); ">
			<option value="-1">
				请选择机构..
			</option>
			<option value="0">
				北京
			</option>
			<option value="1">
				湖南
			</option>
		</select>
		<select name="secOrganIdSel" id="secOrganIdSel">
			<option value="-1">
				请选择机构..
			</option>
		</select>

	</form>
</body>
</html:html>

action文件
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.xiaopeng.struts.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/** 
 * MyEclipse Struts
 * Creation date: 02-20-2009
 * 
 * XDoclet definition:
 * @struts.action parameter="method" validate="true"
 */
public class CascadeAction extends DispatchAction {
	/*
	 * Generated Methods
	 */

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getOrganinfo_ajax(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
//		String level2OrganId=request.getParameter("level2OrganId"); 
//        System.out.print(level2OrganId); 
        response.setContentType("text/xml;charset=UTF-8"); 
        response.setHeader("Cache-control","no-cache"); 
         try {  
            String orgLevel=request.getParameter("orgLevel"); 
            String orgId=  request.getParameter("orgId");
            StringBuffer sb = new StringBuffer(); 
            String str="<select><kkk><value>11111</value><text>北京</text></kkk>"; 
            String str2="<kkk><value>3333</value><text>上海</text></kkk></select>";
            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 
            if(orgId.equals("0")){
            	str="<select><kkk><value>11111</value><text>北京</text></kkk>"; 
            	str2="<kkk><value>2222</value><text>上海</text></kkk></select>";
            }else if(orgId.equals("1")){
            	str="<select><kkk><value>11111</value><text>天津</text></kkk><kkk><value>2222</value><text>四川</text></kkk>"; 
            	str2="<kkk><value>3333</value><text>广州</text></kkk></select>";
            }
            sb.append(str); 
            sb.append(str2);
            System.out.println(sb.toString());
            PrintWriter out=response.getWriter(); 
            out.write(sb.toString()); 
            out.flush(); 
             return   null ; 
        }   catch  (Exception e)  { 
             //  TODO 自动生成 catch 块 
            request.setAttribute("info","获取二级机构异常!"); 
            e.printStackTrace(); 
             return  mapping.getInputForward(); 
        } 

	}
}

你可能感兴趣的:(apache,Ajax,bean,struts,MyEclipse)