在ssh框架中,需要从后台数据库中取出某项业务放在下拉菜单中,然后根据这个业务在另一个下拉菜单中显示这个业务所关联的客户种类,如pojo定义如下: private Integer bsId;
private String bsname;
private String comment;
private Integer remindtype;
private Set gTypes = new HashSet(0);gTypes表示另一张表。我怎样实现出当选中business时,另一个下拉菜单中与business相关联的gtypes也显示出来?
private String bsname;
private String comment;
private Integer remindtype;
private Set gTypes = new HashSet(0);gTypes表示另一张表。我怎样实现出当选中business时,另一个下拉菜单中与business相关联的gtypes也显示出来?
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Ajax示例</title>
<script type="text/javascript">
var request;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
request = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
}
}
function requestGet() {
createXMLHttpRequest();
var business = document.getElementById("business").value;
var queryString = "AjaxRequest?business=" + business;
request.onreadystatechange = handleStateChange;
request.open("get", queryString, true);
request.send(null);
}
function handleStateChange() {
if (request.readyState == 4) {
if (request.status == 200) {
parseResult();
}
}
}
function parseResult() {
var result = request.responseXML;
var type = document.getElementById("type");
while (type.childNodes.length > 0) {
type.removeChild(type.childNodes[0]);
}
var types = result.getElementsByTagName("type");
for (var i = 0; i < types.length; i++) {
var option = document.createElement("option");
var optionValue = types[i].firstChild.nodeValue;
var optionText = document.createTextNode(optionValue);
option.appendChild(optionText);
type.appendChild(option);
}
}
</script>
</head>
<body>
<form action="">
business:
<select id="business" onchange="requestGet()">
<option value="">请选择</option>
<option value="business1">business1</option>
<option value="business2">business2</option>
<option value="business3">business3</option>
</select>
type:
<select id="type"></select>
</form>
</body>
</html>
package com.struts2.servlet;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class AjaxRequest extends HttpServlet { @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
resp.setContentType("text/xml");
String business = req.getParameter("business");
String result = null;
if ("business1".equals(business)) {
// business1的客户种类,这里实际上应该从数据库中查询
String type1 = "AAA";
String type2 = "BBB";
String type3 = "CCC";
result = "<types><type>" + type1 + "</type>" + "<type>" + type2 + "</type>" + "<type>"
+ type3 + "</type>" + "</types>";
}
if ("business2".equals(business)) {
// business2的客户种类,这里实际上应该从数据库中查询
String type1 = "abc";
String type2 = "aaa";
String type3 = "bbb";
result = "<types><type>" + type1 + "</type>" + "<type>" + type2 + "</type>" + "<type>"
+ type3 + "</type>" + "</types>";
}
if ("business3".equals(business)) {
// business3的客户种类,这里实际上应该从数据库中查询
String type1 = "111";
String type2 = "222";
String type3 = "333333";
result = "<types><type>" + type1 + "</type>" + "<type>" + type2 + "</type>" + "<type>"
+ type3 + "</type>" + "</types>";
}
PrintWriter out = resp.getWriter();
out.println(result);
out.close();
}
}这是用Ajax做的,希望对你有所帮助,你可以把数据来源变成从数据库查询得来,我是直接写了几个数据,用来演示!
js得到服务器返回的数据,通过DWR的util.js里的方法也很容易在页面上展示,比如显示在下拉列表或表中,DWR都提供了很方便的方法。