简体   繁体   English

AngularJS和Java Servlet不起作用

[英]AngularJS and Java Servlet does not work

I found a piece of code to use AngularJS with Java Servlet. 我找到了一段将AngularJS与Java Servlet结合使用的代码。

The problem is that when I run the application it tell me that the 'AtpPlayers' page it does not exists and, of course, it does not fill my table. 问题是,当我运行该应用程序时,它告诉我“ AtpPlayers”页面不存在,当然,它也不会填满我的桌子。

I was trying in JBOSS server and json-simple-1.1.1.jar library. 我正在JBOSS服务器和json-simple-1.1.1.jar库中尝试。

What is doing wrong? 怎么了?

The servlet is here: 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);
    }
  }
}

The HTML & JS code is that: 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>

I found the problem: 我发现了问题:

$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');
});

replaced with: 替换为:

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

I don't knows why the guy which write the original code was typed "function (data, status, headers, config)"... 我不知道为什么写原始代码的那个人被打成“函数(数据,状态,标题,配置)”……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM