I have recently started learning Angular JS and I am attempting to combine it with JSP & Servlet. My goal is to create a simple code that retrieves data from an Action Class and displays it on the Index.jsp page. Below is my code:
Index.jsp -
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Fetch Data</title>
<script type="text/javascript" src="js/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope , $http ) {
$scope.getDataFromServer = function() {
alert("Check Function");
$http({
method : 'GET',
url : '/AngularJsServlet'
}).success(function(data, status, headers, config) {
alert('data----'+data);
$scope.person = data;
}).error(function(data, status, headers, config) {
alert("failure");
});
};
});
</script>
</head>
<body>
<div data-ng-app="myApp" data-ng-controller="myCtrl">
<button data-ng-click="getDataFromServer()">Fetch data from server
</button>
<p>First Name : {{person.firstName}}</p>
<p>Last Name : {{person.lastName}}</p>
</div>
</body>
</html>
PersonData.Java (Model Class) -
package com.model;
public class PersonData {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}