java二级联动json数据,二级菜单联动 ajax+json

该博客展示了如何利用Ajax和JSON在Web应用程序中实现实时的二级菜单联动效果。通过监听省份选择的变更事件,动态加载并更新城市列表。当用户选择省份时,后台根据选择的省份返回对应的JSON数据,前端接收到数据后更新城市下拉菜单。示例中涉及了Servlet、Java对象转换为JSON、JavaScript处理JSON响应以及XMLHttpRequest对象的使用。
摘要由CSDN通过智能技术生成

搜索热词

Web应用目录:

具体的代码如下:

AjaxJSON.java

package AjaxJson;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

import net.sf.json.JSONArray;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class AjaxJSON extends HttpServlet {

/**

*

*/

private static final long serialVersionUID = 1L;

/**

* The doGet method of the servlet.

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException {

this.doPost(request,response);

}

/**

* The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException {

response.setContentType("text/html;charset=utf-8");

String province =new String(request.getParameter("province").getBytes("ISO-8859-1"),"UTF-8") ;

System.out.println("后台中的参数省份是:"+province);

List cities = new ArrayList();

JSONArray jsonArray = null; //需要带入Json-lib包;

if ("广东省".equals(province)) {

City city1 = new City(1,"广州");

City city2 = new City(1,"深圳");

City city3 = new City(1,"珠海");

City city4 = new City(1,"佛山");

cities.add(city1);

cities.add(city2);

cities.add(city3);

cities.add(city4);

//把java对象转换为json字符串

jsonArray = JSONArray.fromObject(cities);

}

if("海南省".equals(province))

{

City city1 = new City(1,"海口");

City city2 = new City(1,"三亚");

cities.add(city1);

cities.add(city2);

jsonArray = JSONArray.fromObject(cities);

}

PrintWriter pw = response.getWriter();

pw.write(jsonArray.toString());

System.out.println(jsonArray.toString());

}

}

City.java

packageAjaxJson;

publicclassCity {

privateIntegerid;

privateStringname;

publicCity(Integer id,String name) {

super();

this.id= id;

this.name= name;

}

publicInteger getId() {

returnid;

}

publicvoidsetId(Integer id) {

this.id= id;

}

publicString getName() {

returnname;

}

publicvoidsetName(String name) {

this.name= name;

}

}

Ajax_json.jsp

pageEncoding="UTF-8"%>

Insert title here

选择省份

广东省

海南省

选择城市

document.getElementById("provinceID").οnchange=function()

{

var province = this[this.selectedIndex].innerHTML;

alert("*****************province="+province);

var cityElement = document.getElementById("cityID");

//删除多余的城市,从最后一个开始删除

var size = cityElement.options.length;

if(size>1)

{

for(var i = size-1;i>0;i--)

{

cityElement.removeChild(cityElement.options[i]);

}

}

//加载XMLHttpRequest对象

var xhr = createXHR();

xhr.onreadystatechange=function()

{

if(xhr.readyState==4)

{

if(xhr.status==200)

{

//AJAX引擎读取服务端的信息

var xmlDocument = xhr.responseText;

//将Java语言的String类型转换JavaScript的string类型

var json = eval(xmlDocument);

size = json.length;

for(var i = 0;i

{

//得到每个城市的名称

var city = json[i].name;

var optionElement = document.createElement("option");

optionElement.innerHTML= city;

cityElement.appendChild(optionElement);

}

}

}

}

var method = "POST";

var sendData="province="+province;

var url = "${pageContext.request.contextPath}/AjaxJSON?time="+new Date().getTime();

alert("url="+url);

xhr.open(method,url);

xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");

xhr.send(sendData);

}

//创建XMLHttpRequest

function createXHR()

{

var xhr=null;

try{

//ie6

xhr = new ActiveXObject(microsoft.xmlhttp);

}catch(e)

{

//其他浏览器

xhr = new XMLHttpRequest();

}

return xhr;

}

总结

以上是编程之家为你收集整理的二级菜单联动 ajax+json全部内容,希望文章能够帮你解决二级菜单联动 ajax+json所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值