如何从弹簧控制器获取数据到角度js

时间:2017-11-27 04:23:31

标签: angularjs spring-mvc

这是控制器:

这是我的spring控制器页面,我需要获取我的值并使用angular js.i在我的jsp页面中发布了很多来通过像JSON一样传递来获得这个deatils但它不起作用。请帮助如何获取详细信息控制器。

@RequestMapping(value = "/search", method = RequestMethod.GET)
 public @ResponseBody void  search(HttpServletResponse res,HttpServletRequest req) throws IOException {

 List<Employee> data =  employeeService.listEmployeess();
   JSONArray array = new JSONArray();
       for (Employee e : data) {
           JSONObject jsonObject = new JSONObject(e);
           array.put(jsonObject);
       }
    res.getWriter().append(array.toString());

   }

我的JSP页面:

这是我的jsp页面,我试图将我的数据从控制器传到我的jsp页面,但它不起作用。它也没有抛出任何错误msg.simply数据没有显示。

<!doctype html>
<html >
    <head>
        <title>Spring MVC + AngularJS Demo</title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
        <script>
angular.module("app",[])
        .controller("Hello",function ($scope,$http){
     $scope.getData = function() {
            $http.get('http://localhost:8080/sdnext/search.html').
                success(function(response) {
                    $scope.employees = response.data;
                });
        }
    });

        </script>
    </head>
    <body ng-app="app">

        <table>
        <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Age</th>
        <th>salary</th>
        <th>Address</th>
        <th>BloodGrp</th>
        <th>Aids</th>
        <th>Weight</th>
        </tr>
        <tr>

<tbody  ng-controller="Hello" ng-init="getData ()">
 <tr ng-repeat="employee in employees"> 
 <td>{{employee.empId}}</td>
  <td>{{employee.empName}}</td>
   <td>{{employee.empAge}}</td> 
   <td>{{employee.salary}}</td> 
   <td>{{employee.empAddress}}</td>
    <td>{{employee.bloodgrp}}</td> 
    <td>{{employee.aids}}</td>
     <td>{{employee.weight}}</td> 
     </tr> </tbody> 
            </table>

    </body>
</html>

1 个答案:

答案 0 :(得分:0)

如果$http.get('http ://localhost:8080/search')是获取

数据的正确网址
[
   {"empId":1,"bloodgrp":"0-ve","empName":"krishnaKumars","wei‌​ght":78,"aids":"nega‌​tive","empAge":23,"s‌​alary":15000,"empAdd‌​ress":"madurai"},
   {"e‌​mpId":2,"bloodgrp":"‌​o-ve","empName":"Arc‌​hanasundar","weight"‌​:68,"aids":"Negative‌​","empAge":31,"salar‌​y":50000,"empAddress‌​":"chennai"}
]

然后像这样写

$http.get('http ://localhost:8080/search')
          .success(function(response) {
                $scope.employees = response;
            });
    }

你正在使用非常旧版本的AngularJs。请转到最新版本,如1.6.5,然后你必须写像

$http.get('http ://localhost:8080/search')
          .then(function(response) {
                $scope.employees = response.data;
            });
    }