首先需要把以下JS粘到 jsp页面的javascript标签中:
<script type="text/javascript">
var xmlHttp;
var xmlDOM=new ActiveXObject("Microsoft.XMLDOM");
xmlDOM.async=false;//是否异步更新
//创建XMLHttpRequest对象
function createXMLHttpRequest(){
if(window.ActiveXObject){
//如果用户使用IE,就返回XMLHTTP的ActiveX对象
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
//否则返回一个XMLHttpRequest对象
xmlHttp = new XMLHttpRequest();
}
}
function getregion(){
createXMLHttpRequest();
try{//document.form1.d_device_id
//post方式传递参数
xmlHttp.onreadystatechange = handleStateGetRetion;
var d_device_id=document.getElementById("d_device_id").value;
var post="d_device_id="+d_device_id;
//alert(d_device_id);
xmlHttp.open ('post',"<%=path%>/getregion.do",true);
xmlHttp.setrequestheader("cache-control","no-cache");
xmlHttp.setrequestheader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send (post);//注意:POST方式,使用这个来发送内容?
}catch(exception){
alert("您要访问的资源不存在!");
}
}
function handleStateGetRetion(){
if(xmlHttp.readyState == 4){
if (xmlHttp.status == 200 || xmlHttp.status == 0){
// 取得XML的DOM对象
var res=xmlHttp.responseXML.getElementsByTagName("res")
//alert(res);
var list = document.all.d_ifindex;
list.options.length=0;
list.add(new Option("---请选择---",""));
for(var i=0;i<res.length;i++){
list.add(new Option(res[i].firstChild.data,res[i].firstChild.data));
}
}
}
}
</script>
下面是HTML代码(本人只选取其中2个下拉菜单拿过来做演示例子):
<tr bgcolor="#E3F3FD">
<td align="right">主动标识:</td>
<td><label>
<select id="d_device_id" name="d_device_id" class="select" onChange="getregion();">
<option value="">--请选择--</option>
<%=(String)request.getAttribute("str") %>
[color=red]这里是第一个菜单中(主动菜单)的选项,在这里我就不给大家把这里列出来了.可以查询出一个带“<option>”标签的String字符串,到这得到便可[/color]
</select>
</label>
<label></label></td>
</tr>
<tr bgcolor="#E3F3FD">
<td align="right">目标联动标识: </td>
<td> <label>
<select id="d_ifindex" name="d_ifindex" class="select">
</select>
</label>
</td>
</tr>
再下边就是后台的struts类:
package com.snmp.action;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.snmp.util.DB;
/**
*
* 联动下拉菜单
* @author 徐彬
* 2009-06-25
*
*/
public class GetRegionAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
response.setContentType("text/xml; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control", "no-cache");
List cities = new ArrayList();
PrintWriter out = response.getWriter();
String d_device_id = request.getParameter("d_device_id");
try {
conn = DB.getConnection();
stmt = conn.createStatement();
String sql = "select device_id, ifindex, port_desc, speed from snmp_port where device_id='"+d_device_id+"'";
//String strtwo=DB.getSelectOption(sql, "");
rs=stmt.executeQuery(sql);
while (rs.next())
{
cities.add(rs.getString(2));
}
DB.releaseCursor(conn, stmt, rs);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println("<response>");
for(int i=0;i<cities.size();i++)
{
out.println("<res>" + cities.get(i).toString() + "</res>");
}
out.println("</response>");
out.close();
return null;
}
}
<script type="text/javascript">
var xmlHttp;
var xmlDOM=new ActiveXObject("Microsoft.XMLDOM");
xmlDOM.async=false;//是否异步更新
//创建XMLHttpRequest对象
function createXMLHttpRequest(){
if(window.ActiveXObject){
//如果用户使用IE,就返回XMLHTTP的ActiveX对象
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
//否则返回一个XMLHttpRequest对象
xmlHttp = new XMLHttpRequest();
}
}
function getregion(){
createXMLHttpRequest();
try{//document.form1.d_device_id
//post方式传递参数
xmlHttp.onreadystatechange = handleStateGetRetion;
var d_device_id=document.getElementById("d_device_id").value;
var post="d_device_id="+d_device_id;
//alert(d_device_id);
xmlHttp.open ('post',"<%=path%>/getregion.do",true);
xmlHttp.setrequestheader("cache-control","no-cache");
xmlHttp.setrequestheader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send (post);//注意:POST方式,使用这个来发送内容?
}catch(exception){
alert("您要访问的资源不存在!");
}
}
function handleStateGetRetion(){
if(xmlHttp.readyState == 4){
if (xmlHttp.status == 200 || xmlHttp.status == 0){
// 取得XML的DOM对象
var res=xmlHttp.responseXML.getElementsByTagName("res")
//alert(res);
var list = document.all.d_ifindex;
list.options.length=0;
list.add(new Option("---请选择---",""));
for(var i=0;i<res.length;i++){
list.add(new Option(res[i].firstChild.data,res[i].firstChild.data));
}
}
}
}
</script>
下面是HTML代码(本人只选取其中2个下拉菜单拿过来做演示例子):
<tr bgcolor="#E3F3FD">
<td align="right">主动标识:</td>
<td><label>
<select id="d_device_id" name="d_device_id" class="select" onChange="getregion();">
<option value="">--请选择--</option>
<%=(String)request.getAttribute("str") %>
[color=red]这里是第一个菜单中(主动菜单)的选项,在这里我就不给大家把这里列出来了.可以查询出一个带“<option>”标签的String字符串,到这得到便可[/color]
</select>
</label>
<label></label></td>
</tr>
<tr bgcolor="#E3F3FD">
<td align="right">目标联动标识: </td>
<td> <label>
<select id="d_ifindex" name="d_ifindex" class="select">
</select>
</label>
</td>
</tr>
再下边就是后台的struts类:
package com.snmp.action;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.snmp.util.DB;
/**
*
* 联动下拉菜单
* @author 徐彬
* 2009-06-25
*
*/
public class GetRegionAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
response.setContentType("text/xml; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control", "no-cache");
List cities = new ArrayList();
PrintWriter out = response.getWriter();
String d_device_id = request.getParameter("d_device_id");
try {
conn = DB.getConnection();
stmt = conn.createStatement();
String sql = "select device_id, ifindex, port_desc, speed from snmp_port where device_id='"+d_device_id+"'";
//String strtwo=DB.getSelectOption(sql, "");
rs=stmt.executeQuery(sql);
while (rs.next())
{
cities.add(rs.getString(2));
}
DB.releaseCursor(conn, stmt, rs);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println("<response>");
for(int i=0;i<cities.size();i++)
{
out.println("<res>" + cities.get(i).toString() + "</res>");
}
out.println("</response>");
out.close();
return null;
}
}