http://topic.csdn.net/u/20080802/11/0829065c-0187-4d53-a120-efd493d158e5.html
使用prototype框架 list使用json对象传输:
<html>
<head>
<title>Insert title here </title>
<script src="js/prototype.js"> </script>
<script language="javascript">
function searchSales2(){
var country=$F('country');
var url='http://localhost:8080/HelloWorld/AjaxServlet';
var pars=country='+country;
var myAjax=new Ajax.Request(url,{
method:'post',parameters:pars,onComplete:showResponse
});
}
function showResponse(originalRequest)
{
var jsonRaw = originalRequest.responseText.evalJSON(true);
$('language1').options.length=0;
jsonRaw.each(function(myobj1){
$('language1').add(new Option(myobj1.language,myobj1.language));
});
}
</script>
</head>
<body>
<form name="form1" method="post" οnlοad="onloadHandler()">
<select id="country" name="country" οnchange="searchSales2()">
<option value="ABW">Aruba </option>
<option value="AFG">Afghanistan </option>
<option value="AGO">Angola </option>
</select>
<select id="language1" name="language1">
<option value="0">select one </option>
</select>
</form>
</body>
</html>
servlet:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String country= request.getParameter("country"); //请求国家
Object obj = ctx.getBean(reqClass);
Class c2 = obj.getClass();
try {
CountryService service = new CountryService();
List list = service.getCountryLanguageList(country);
String outobj = json2(list);
response.getWriter().print(outobj);//把json数组返回前台
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//把查询的list列表放入json数组
private String json2(List list) throws JSONException {
JSONArray resultArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
for (int i = 0; i < list.size(); i++) {
Countrylanguage cl = (Countrylanguage) list.get(i);
jsonObject = this.getDomainJSON(cl);
resultArray.put(jsonObject);
}
return resultArray.toString();
}
//把国家代号和语言放入json对象
private JSONObject getDomainJSON(Countrylanguage cl) throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("countryCode", cl.getCountryCode());
jsonObject.put("language", cl.getLanguage());
return jsonObject;
使用prototype框架 list使用json对象传输:
<html>
<head>
<title>Insert title here </title>
<script src="js/prototype.js"> </script>
<script language="javascript">
function searchSales2(){
var country=$F('country');
var url='http://localhost:8080/HelloWorld/AjaxServlet';
var pars=country='+country;
var myAjax=new Ajax.Request(url,{
method:'post',parameters:pars,onComplete:showResponse
});
}
function showResponse(originalRequest)
{
var jsonRaw = originalRequest.responseText.evalJSON(true);
$('language1').options.length=0;
jsonRaw.each(function(myobj1){
$('language1').add(new Option(myobj1.language,myobj1.language));
});
}
</script>
</head>
<body>
<form name="form1" method="post" οnlοad="onloadHandler()">
<select id="country" name="country" οnchange="searchSales2()">
<option value="ABW">Aruba </option>
<option value="AFG">Afghanistan </option>
<option value="AGO">Angola </option>
</select>
<select id="language1" name="language1">
<option value="0">select one </option>
</select>
</form>
</body>
</html>
servlet:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String country= request.getParameter("country"); //请求国家
Object obj = ctx.getBean(reqClass);
Class c2 = obj.getClass();
try {
CountryService service = new CountryService();
List list = service.getCountryLanguageList(country);
String outobj = json2(list);
response.getWriter().print(outobj);//把json数组返回前台
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//把查询的list列表放入json数组
private String json2(List list) throws JSONException {
JSONArray resultArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
for (int i = 0; i < list.size(); i++) {
Countrylanguage cl = (Countrylanguage) list.get(i);
jsonObject = this.getDomainJSON(cl);
resultArray.put(jsonObject);
}
return resultArray.toString();
}
//把国家代号和语言放入json对象
private JSONObject getDomainJSON(Countrylanguage cl) throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("countryCode", cl.getCountryCode());
jsonObject.put("language", cl.getLanguage());
return jsonObject;