私はJava EEと角型JSの初心者です。ウェブサイトからコードを見つけました(http://www.simplecodestuffs.com/angularjs-interacting-with-java-servlet-using-json/) 私はEclipseで実装しようとしています。ここ旧AngularJSデモが動作しない
First Name: {{person.firstName}}
Last Name: {{person.lastName}}
は私のJSPである:ここ
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>AJAX with Servlets using AngularJS</title>
<script type="text/javascript" src="jas/angular.min.js"> </script>
<script>
var app = angular.module('myApp', []);
function MyController($scope, $http) {
$scope.getDataFromServer = function() {
$http({
method:'GET',
url:'http://localhost:8080/Angular/AngularJsServlet'
}).success(function(data, status, headers, config) {
$scope.person = data;
}).error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
};
};
</script>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="MyController">
<button ng-click="getDataFromServer()"> Fetch data from server </button>
<p>First Name: {{person.firstName}}</p>
<p>Last Name: {{person.lastName}}</p>
</div>
</div>
</body>
</html>
は私のサーブレットです:
が、私はボタンをクリックしたときに出力を "サーバーからデータをフェッチするには、" 予想出力として表示されますdoesnot
@WebServlet("/AngularJsServlet")
public class AngularJsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AngularJsServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PersonData personData = new PersonData();
personData.setFirstName("Mohaideen");
personData.setLastName("Jamil");
String json = new Gson().toJson(personData);
response.setContentType("application/json");
response.getWriter().write(json);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request, response);
}
}