AngularJS和Java Servlet不起作用

时间:2016-09-09 09:51:54

标签: java angularjs servlets server

我找到了一段代码,将AngularJS与Java Servlet一起使用。

问题在于,当我运行应用程序时,它告诉我'AtpPlayers'页面它不存在,当然,它不会填满我的表。

我在JBOSS服务器和json-simple-1.1.1.jar库中尝试。

出错了什么?

servlet在这里:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/AtpPlayers")
public class AtpPlayers extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("application/json;charset=UTF-8");
        try {
            PrintWriter out = response.getWriter();
            out.println("[{\"name\": \"Nadal, Rafael (ESP)\", \"email\": \"nadalrafael@gmail.com\", \"rank\": \"1\"},"
                + "{\"name\": \"Djokovic, Novak (SRB)\", \"email\": \"djokovicnovak@gmail.com\", \"rank\": \"2\"},"
                + "{\"name\": \"Federer, Roger (SUI)\", \"email\": \"federerroger@gmail.com\", \"rank\": \"3\"},"
                + "{\"name\": \"Wawrinka, Stan (SUI)\", \"email\": \"wawrinkastan@gmail.com\", \"rank\": \"4\"},"
                + "{\"name\": \"Ferrer, David (ESP)\", \"email\": \"ferrerdavid@gmail.com\", \"rank\": \"5\"}]");
    } catch (Exception e) {
        System.out.println(e);
    }
  }
}

HTML& JS代码就是:

<!DOCTYPE html>
<html>
    <head>    
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">      
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>      
    </head>
    <body ng-app="ATP_PLAYERS">  
        <div ng-controller="atpController">
            <h5>Loading ATP players from a Servlet</h5>
            <table>
                <thead>
                    <tr>
                        <th>Rank</th>
                        <th>Name</th>
                        <th>E-mail</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="item in atp">
                        <td>{{item.rank}}</td>            
                        <td>{{item.name}}</td>
                        <td>{{item.email}}</td>
                    </tr>
                </tbody>
            </table>
        </div>    

        <script language="javascript" type="text/javascript">
          angular.module('ATP_PLAYERS', [])
           .controller('atpController', function ($scope, $http) {
              $http.get('http://localhost:8080/AngularGET/AtpPlayers').then(function (data, status, headers, config) {
                $scope.atp = data;
                alert('OK');
             }, function (data, status, headers, config) {
                alert('NU');
               });
         });
        </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

我发现了问题:

$http.get('http://localhost:8080/AngularGET/AtpPlayers').then(function (data, status, headers, config) {
    $scope.atp = data;
    alert('OK');
}, function (data, status, headers, config) {
    alert('NU');
});

替换为:

$http.get('http://localhost:8080/AngularGET/AtpPlayers').then(function (response) {
    $scope.atp = response.data;
    alert('OK');
}, function (response) {
    alert('NU');
});

我不知道为什么编写原始代码的人输入了“function(data,status,headers,config)”...