简体   繁体   中英

AngularJS and Java Servlet does not work

I found a piece of code to use AngularJS with 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.

I was trying in JBOSS server and json-simple-1.1.1.jar library.

What is doing wrong?

The servlet is here:

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:

<!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)"...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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