ajax+spring mvc 实现省市二级联动

后台代码如下

/**
	 * 访问首页并加载省信息
	 * @return
	 */
	@RequestMapping("/index.shtml")
	public String index( Model model){
		List list = provinceServince.findAllProvince();
		model.addAttribute("provinces", list);
		return "index";
	}
	
	/**
	 * 查询城市
	 * @return
	 */
	//@RequestParam(value="proid")
	@RequestMapping("/queryCity.shtml")
	@ResponseBody
	public void queryCity(Integer proid,HttpServletResponse response,Model model){
		try {
			List list = cityService.findCityByProvince(proid);
			JSONArray json = JSONArray.fromObject(list);
			System.out.println("查询城市");
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;character=utf-8");
			response.getWriter().println(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	

页面代码如下

选择省:
		
		选择市:
		

js代码如下


其实如果用springmvc的话可以把省信息在controller带到页面遍历,市下拉框在用ajax做联动比较省事

你可能感兴趣的:(ajax)