servlet用post处理json装的数据到jsp中,怎样装到select的option。做一个二级联动
UserServlet.java处理前台请求publicvoiddoPost(HttpServletRequestrequest,HttpServletResponser...
UserServlet.java 处理前台请求
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
System.out.println("进入servlet");
String json="{'city':[{'cityId':'1','cityName':'重庆'},{'cityId':'2','cityName':'四川'},{'cityId':'3','cityName':'北京'}]," +
"'area':[{'cityId':'1','areaId':'1','areaName':'沙坪坝'},{'cityId':'1','areaId':'2','areaName':'江北区'},{'cityId':'1','areaId':'3','areaName':'渝北区'}," +
"{'cityId':'2','areaId':'4','areaName':'绵阳区'},{'cityId':'2','areaId':'5','areaName':'成都区'},{'cityId':'2','areaId':'6','areaName':'南充区'}," +
"{'cityId':'3','areaId':'7','areaName':'朝阳区'},{'cityId':'3','areaId':'8','areaName':'海定区'},{'cityId':'3','areaId':'9','areaName':'中关村区'}]}";
response.getWriter().print(json);
} 展开
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
System.out.println("进入servlet");
String json="{'city':[{'cityId':'1','cityName':'重庆'},{'cityId':'2','cityName':'四川'},{'cityId':'3','cityName':'北京'}]," +
"'area':[{'cityId':'1','areaId':'1','areaName':'沙坪坝'},{'cityId':'1','areaId':'2','areaName':'江北区'},{'cityId':'1','areaId':'3','areaName':'渝北区'}," +
"{'cityId':'2','areaId':'4','areaName':'绵阳区'},{'cityId':'2','areaId':'5','areaName':'成都区'},{'cityId':'2','areaId':'6','areaName':'南充区'}," +
"{'cityId':'3','areaId':'7','areaName':'朝阳区'},{'cityId':'3','areaId':'8','areaName':'海定区'},{'cityId':'3','areaId':'9','areaName':'中关村区'}]}";
response.getWriter().print(json);
} 展开
展开全部
其实,在项目里,放入json相关的jar包,就可以了
再把要显示的内空封装成一个对象
如:public class City{
private int cityId;
private String cityName;
get..set方法略
}
再把需要显示的放到对象放到list集合里
再
JSONArray json=JSONArray.fromObject(“集合变量名”);
response.getWriter().print(json);
就可以回到前台了
再把要显示的内空封装成一个对象
如:public class City{
private int cityId;
private String cityName;
get..set方法略
}
再把需要显示的放到对象放到list集合里
再
JSONArray json=JSONArray.fromObject(“集合变量名”);
response.getWriter().print(json);
就可以回到前台了
追问
怎样把jsp的数据绑定到页面上,我试了很多次都不得行。缓存清空了。重新加载也不行。就差咋电脑了。var obj=eval('('+result+')');//转换成json的对象for 循环时绑定不了数据到option
var citys=document.getElementById("city");
for(var i=0;i<arrs.length;i++){
alert(arrs[i].cityId+arrs[i].cityName);var option=new Option(arrs[i].cityName,arrs[i]cityId) citys.add(option);}
追答
$("#select2").change(function(){
$.ajax({
url:"action地址",
type:'post',
dataType:'json',
success:function(json){
json=eval(json);
$("#select2").children().remove();
$(json).each(funciton(i){
$("#select2").append(""+json[i].cityName+"");
});
}
});
});
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询