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

古瓦利乌尔

这是控制器:

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

@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>
湿婆克里希纳

如果$http.get('http ://localhost:8080/search')是获取数据的正确 URL

[
   {"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;
            });
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章