使用prototype框架 list使用json对象传输:

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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值