index.html
<!DOCTYPE html> <html> <head> <Meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>AngularJS for login </title> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> <script src=JS/city.js></script> <style> table,th,td { border: 1px solid grey; border-collapse: collapse; padding: 3px; } table tr:nth-child(odd) { background-color: #f1f1f1; } table tr:nth-child(even) { background-color: #ffffff; } </style> </head> <body> <div ng-app="myApp" ng-controller = "myAppCtrl"> <table> <tr ng-repeat="x in res"> <td>{{ x.province }}</td> <td>{{ x.city }}</td> </tr> </table> </div> </body> </html>city.js
var app = angular.module('myApp',[]); app.controller('myAppCtrl',function($scope,$http) { $scope.res = "测试"; $http.get("admin/getcitys") .success(function(res) { $scope.res = res ; }) ; });
package com.liyang.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.google.gson.Gson; @Controller @RequestMapping(value = "/admin") public class AdminController { @RequestMapping(value = "getcitys",produces="text/html;charset=UTF-8") @ResponseBody public String getCitys(){ Map<String,String> city = new HashMap<String,String>() ; city.put("北京市","北京"); city.put("上海市","上海"); city.put("天津市","天津"); city.put("重庆市","重庆"); city.put("黑龙江省","哈尔滨"); city.put("吉林省","长春"); city.put("辽宁省","沈阳"); city.put("内蒙古自治区","呼和浩特"); city.put("河北省","石家庄"); city.put("新疆维吾尔自治区","乌鲁木齐"); city.put("甘肃省","兰州"); city.put("青海省","西宁"); city.put("陕西省","西安"); city.put("宁夏回族自治区","银川"); city.put("河南省","郑州"); city.put("山东省","济南"); city.put("山西省","太原"); city.put("安徽省","合肥"); city.put("湖南省","长沙"); city.put("湖北省","武汉"); city.put("江苏省","南京"); city.put("四川省","成都"); city.put("贵州省","贵阳") ; city.put("云南省","昆明") ; city.put("广西壮族自治区","南宁") ; city.put("西藏自治区","拉萨") ; city.put("浙江省","杭州") ; city.put("江西省","南昌") ; city.put("广东省","广州") ; city.put("福建省","福州") ; city.put("台湾省","台北") ; city.put("海南省","海口") ; city.put("香港特别行政区","香港") ; city.put("澳门特别行政区","澳门") ; List<Map<String,String> > res = new ArrayList<Map<String,String>>() ; for(Map.Entry<String,String> e : city.entrySet()){ Map<String,String> c = new HashMap<String,String>() ; c.put("province",e.getKey()) ; c.put("city",e.getValue()) ; res.add(c) ; } Gson gson = new Gson() ; return gson.toJson(res) ; } }