Struts下拉框联动问题(从数据库中取)
请问怎么才能用struts实现下拉框的联动呢 问题点数:20、回复次数:9Top
1 楼terry6394(小猪,向前跑!)回复于 2006-07-01 14:35:34 得分 0
ajax行不?!Top
2 楼suyu_001(勇者无谓)回复于 2006-07-01 19:34:49 得分 0
写一个bean,在bean里读数据库了的值 把值放到org.apache.struts.util.LabelValueBean类里去在把这个类放到一个vector里或arraylist里并做为返回值返回给调用这个方法的对象。
在jsp里把获得的vector对象或arraylist对象用pageContext.setAttribute(string name,object object)设置一下
<html:select property="accountTypeId">
<html:options collection="[name]" property="value" labelProperty="label"/>
</html:select>
name是你用setAttribute设置的名字。Top
3 楼lfaviate()回复于 2006-07-03 08:36:19 得分 0
但这样不可以实现二级联动啊Top
4 楼rickhunterchen(千山鸟飞绝)回复于 2006-07-03 09:09:03 得分 20
这是我的项目中的代码,你可以参考一下:
<html>
<head>
<title> </title>
<link rel = "stylesheet" type = "text/css" href = "../css/olstyle.css">
<SCRIPT type="text/javascript">
var req;
window.onload=function(){}
function Change_Select()
{
var zhi=document.getElementById('m_gykfwzlbb_lbbm').value;
var url="sqdSelect.go?method=getSkill&id="+zhi;
if(zhi=="0"){
alert("请选择您要察看的信息");
return;
}else{
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;
req.send(null);
}
}
}
function Change_Select2()
{
var zhi=document.getElementById('m_lbbm_se').value;
//alert(zhi.substring(0,2));
if(zhi=="0"){
alert("请选择您要察看的信息");
return;
}
if(zhi.substring(0,2)=="WY"){
var url="sqdSelect.go?method=getSkill2&id="+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=callback2;
req.send(null);
}
}else{
parent.topFram.location ="sqdSelectAll.go?method=getlistAll3&id="+zhi+"&&hzbmField=<%=hzbmField%>&&ggxhField=<%=ggxhField%>&&jldwField=<%=jldwField%>&&wzbmmcField=<%=wzbmmcField%>&&wzbmField=<%=wzbmField%>&&formName=<%=formName%>";
}
}
function Change_Select3()
{
var zhi=document.getElementById('m_lbbm_th').value;
//alert(zhi.substring(0,2));
if(zhi=="0"){
alert("请选择您要察看的信息");
return;
}else{
parent.topFram.location ="sqdSelectAll.go?method=getlistAll4&id="+zhi+"&&hzbmField=<%=hzbmField%>&&ggxhField=<%=ggxhField%>&&jldwField=<%=jldwField%>&&wzbmmcField=<%=wzbmmcField%>&&wzbmField=<%=wzbmField%>&&formName=<%=formName%>";
}
}
function callback()
{
if(req.readyState == 4)
{
if(req.status == 200)
{
parseMessage();
}else{
alert("Not able to retrieve description"+req.statusText);
}
}
}
function callback2()
{
if(req.readyState == 4)
{
if(req.status == 200)
{
parseMessage2();
}else{
alert("Not able to retrieve description"+req.statusText);
}
}
}
function parseMessage()
{
var xmlDoc=req.responseXML.documentElement;
var xSel=xmlDoc.getElementsByTagName('select');
var select_root=document.getElementById('m_lbbm_se');
select_root.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);
try{
select_root.add(option);
}catch(e){
}
}
}
function parseMessage2()
{
var xmlDoc=req.responseXML.documentElement;
var xSel=xmlDoc.getElementsByTagName('select');
var select_root=document.getElementById('m_lbbm_th');
select_root.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);
try{
select_root.add(option);
}catch(e){
}
}
}
</SCRIPT>
</head>
<body bgcolor = "#C8D0D4">
<html:form action="/cgsqdNewAction.go">
<TABLE class = "cbToolbar" id = "idToolbar" cellpadding = '0' cellspacing = '0'>
<TR align = "left" valign = "top" >
<TD NOWRAP> 物质类别:
<html:select property="m_gykfwzlbb_lbbm" onchange="Change_Select()">
<html:option value="0">请选择</html:option>
<html:options collection="LbfList" property="m_gykfwzlbb_lbbm" labelProperty="m_gykfwzlbb_lbmc"/>
</html:select>
<html:select property="m_lbbm_se" styleId="m_lbbm_se" onchange="Change_Select2()">
<html:option value="0"> </html:option>
</html:select>
<html:select property="m_lbbm_th" styleId="m_lbbm_th" onchange="Change_Select3()">
<html:option value="0"> </html:option>
</html:select>
</TD>
</tr>
</table>
</html:form>
</body>
</html>Top
5 楼imA(男的不会,会的不男)回复于 2006-07-03 09:30:10 得分 0
给你一个结合AJAX的例子(没有用struts,你可以把下面的servlet改成action)
前台页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>??</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">
<script>
var xmlHttp;
function valueChange(val)
{
alert("hhelo");
if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}else if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
if(!xmlHttp)
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
}
url="http://localhost:8080/ws/LDServlet?parent="+val;
xmlHttp.onreadystatechange=function()
{
//alert("AAAAAAAAA");
if(xmlHttp.readystate==4)
{
if(xmlHttp.status==200)
{
xmlDOM=xmlHttp.responseXML;
root=xmlDOM.documentElement;
htmlstr="<select>";
alert(root.childNodes.length);
while(root.hasChildNodes())
{
var child=root.firstChild;
htmlstr+="<option value='"+child.getAttribute("id")+"'>"+child.getAttribute("value")+"</option>";
root.removeChild(child);
}
htmlstr+="</select>";
document.getElementById("child").innerHTML=htmlstr;
}
}
};
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(null);
}
</script>
</head>
<body>
<table>
<tr><td>????
<select onchange="valueChange(this.value)">
<option value=""></option>
<option value="hello">hello</option>
<option value="else">else</option>
<option value="whatever">else</option>
</select>
</td>
<td>????
<div id="child"></div>
</td></tr>
</table>
</body>
</html>
后台servlet:
package cn.forecast.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class for Servlet: LDServlet
*
*/
public class LDServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public LDServlet() {
super();
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String parent=request.getParameter("parent");
System.out.println("parent: "+parent);
response.setHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 0);
response.setContentType("text/xml; charset=GB2312");
try
{
org.w3c.dom.Document document=javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
org.w3c.dom.Element root=document.createElement("root");
org.w3c.dom.Element child1=document.createElement("child1");
org.w3c.dom.Element child2=document.createElement("child2");
if(parent.equals("hello")){
child1.setAttribute("id","child1");
child1.setAttribute("value","孩子1");
child2.setAttribute("id","child2");
child2.setAttribute("value","孩子2");
}else if(parent.equals("else"))
{
child1.setAttribute("id","child1");
child1.setAttribute("value","辽宁");
child2.setAttribute("id","child2");
child2.setAttribute("value","吉林");
}else if(parent.equals("whatever"))
{
child1.setAttribute("id","child1");
child1.setAttribute("value","共和党");
child2.setAttribute("id","child2");
child2.setAttribute("value","民主党");
}
root.appendChild(child1);
root.appendChild(child2);
document.appendChild(root);
javax.xml.transform.dom.DOMSource doms=new javax.xml.transform.dom.DOMSource(document);
javax.xml.transform.stream.StreamResult sr=new javax.xml.transform.stream.StreamResult(response.getOutputStream());
javax.xml.transform.TransformerFactory tf=javax.xml.transform.TransformerFactory.newInstance();
javax.xml.transform.Transformer t=tf.newTransformer();
t.setOutputProperty("encoding","GB2312");
t.transform(doms,sr);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
你可以把这个servlet改造成action(因为action也是servlet)。
其实这个问题CSDN上有很多人问过,光我回答的就有5个人之多,只要搜索一下就能找到很多例子的。Top
6 楼imA(男的不会,会的不男)回复于 2006-07-03 09:32:30 得分 0
你取数据库的代码就可以在servlet中执行。Top
7 楼syjava(兼职挂大白)回复于 2006-07-03 09:32:49 得分 0
xxTop
8 楼lfaviate()回复于 2006-07-03 20:12:05 得分 0
谢谢大家 问题已经解决Top
9 楼treeway(趣玩—Trek-Seeking My Way)回复于 2006-07-03 20:23:34 得分 0
m...Top




