日期:2014-05-17  浏览次数:20711 次

如何从Action返回数据到页面中的select控件?
现在正做一个小网站,页面中有三个select,分别是select1,select2,select3.根据select1选中的值,传到Action中,然后在数据库中查询到相应的数据(是一个List<String>类型)返回到这个页面,显示在select2中。同样根据select2中选中的值,查询数据库,将结果显示在select3中?我是一个新手,用的是hibernate3.0+Struts 1.2,没有ActionContext类。不想用script在页面中连接数据库。请各位能够详细说一下,贴出代码更好,谢谢!

------解决方案--------------------
可以去了解下类似“省市联动”的例子
------解决方案--------------------
<select name="xxx" id="xxx" value="${xxx}">
<c:forEach items="${users}" var="user">
<option value="${user.getID()}" ${xxx==user.getID()?'selected':''>${user.getUsername()}</option>
</c:forEach>
</select>
users列表应该是你select2的选项,你通过Action把users列表传过来,就可以动态生成选项了吧
------解决方案--------------------
action 先取页面第一个select标签的值 根据这个值找出第二个标签的选项 放到一个list 里面
最后request.setAttribute("users", list) 然后页面按9楼得方法取就行了
------解决方案--------------------
没有用structs,你可以改改

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'province.jsp' starting page</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">
-->
<script type="text/javascript">
function loadXMLDoc()
{
xmlhttp = null;
var country = document.getElementById("country");
country.length = 1;
country.selectedIndex = 0;
var province = document.getElementById("province");
if(province.value == ""){
return ;
}
var url="http://localhost:8080/Ajax_Province/GetProvince?&province="+encodeURIComponent(province.value);
if (window.XMLHttpRequest) {// code for Firefox, Mozilla, IE7, etc.
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp != null) {
xmlhttp.onreadystatechange = state_Change;
xmlhttp.open("post", url, true);
xmlhttp.send(null);
} else {
alert("Your browser does not support XMLHTTP.");
}
}

function state_Change() {
var province = document.getElementById("province"); 
var country = document.getElementById("country");
if (xmlhttp.readyState == 4&&xmlhttp.status == 200) {// 4 = "loaded"
var serviceData = xmlhttp.responseText;
if(serviceData == null||serviceData == ""){
return;
}
var s = serviceData.split(",");
for(var i=0;i<s.length-1;i++){
country.options[i+1] = new Option(s[i],s[i]);
}
}
}




</script>
</head>