Struts ajax multi-level drop-down menu
First look at the project:
Check the jar package to be added.
In addition to the struts core, we also need these two
Okay. First look at struts. xml.
/testSelect.jsp
municipalities
Let's look at the class used to process requests.
Package cdm. module. file. action; import java. util. arrayList; import java. util. list; import com. opensymphony. xwork2.ActionSupport; public class SearchFileAction extends ActionSupport {/*****/private static final long serialVersionUID =-67000054284459457436l; public int provinceId; public List
Municipalities; public List
Provinces = new ArrayList
(); Public String getProvince () {// The following is the simulated database operation Province p1 = new Province (); p1.setId (1); p1.setName ("Shaanxi Province "); province p2 = new Province (); p2.setId (2); p2.setName ("Shanxi Province"); provinces. add (p1); provinces. add (p2); return SUCCESS;} public String getMunicipality () {municipalities = new ArrayList
(); // The following is the simulated database operation if (provinceId = 1) {Municipality municipality = new Municipality (); municipality. setId (1); municipality. setName ("Xianyang City"); municipality. setProvinceId (1); municipalities. add (municipality); municipality = new Municipality (); municipality. setId (2); municipality. setName ("Xi'an"); municipality. setProvinceId (1); municipalities. add (municipality);} if (provinceId = 2) {Municipality municipality = new Municipality (); municipality. setId (1); municipality. setName ("Yuncheng"); municipality. setProvinceId (2); municipalities. add (municipality); municipality = new Municipality (); municipality. setId (2); municipality. setName ("Taiyuan City"); municipality. setProvinceId (2); municipalities. add (municipality);} System. out. println (municipalities. get (0 ). getName () + "sdfs"); return SUCCESS;} // The getset Method for omitting three instance variables}
As for Municipality and Province, there is an id and name in the Province. Besides the id and name, the city also has a provinceId to indicate the Province in which it is located.
TestSelect. jsp is also a simple example of the following code.
<%@ page contentType="text/html; charset=GB2312"%><%@taglib prefix="s" uri="/struts-tags"%>
<Script type = "text/javascript" src = "js/json2.js"> </script> <script type = "text/javascript" src = "js/prototype. js "> </script> <script type =" text/javascript "src =" js/findInf. js "> </script>
| Provincial Level |
City Level |
|
|
The findInf. js Code introduced by the core is as follows:
Function getProvince (obj) {var provinceId = obj. options [obj. selectedIndex]. value; var select = document. getElementById ("conditionId0"); if (provinceId <0) {select. innerHTML = ""; select1.innerHTML = ""; var con = new Option (); con. text = "-select province first-"; con. value = "-1"; var mat = new Option (); mat. text = "-select province first-"; mat. value = "-1"; select. options. add (con); select. disabled = true; return;} var url = "GetMunicipality. action? ProvinceId = "+ provinceId; new Ajax. request (url, {method: "post", onComplete: function (request) {select. innerHTML = ""; var o = new Option (); o. text = "-select city-"; o. value = "-1"; select. options. add (o); var tableList = JSON. parse (request. responseText); for (var I = 0; I <tableList. length; I ++) {var _ o = new Option (); _ o. text = tableList [I]. name; _ o. value = tableList [I]. id; select. options. add (_ o);} select. disabled = false ;}, asynchronous: true });}Finished!
Several Problems Encountered
<script type="text/javascript" src="js/json2.js"></script> <script type="text/javascript" src="js/prototype.js"></script> <script type="text/javascript" src="js/findInf.js"></script>
FindInf can be referenced only after the above two introductions.
This was not noticed during replication.
Enable the debugging function of firefox. This problem occurs only when Ajax is not define.