How to call java controller class from angularjs
I have a web project..no service call but there is a Java controller where one of the methods returns a string .. this needs to be read in my js file .. I cannot get the data or remove the url mapped to the controller class.
Controller class:
@Controller
@RequestMapping("/user")
public class UserController {
@GET
@RequestMapping(method = RequestMethod.GET, value = "/details")
public @ResponseBody String getUserDetails(HttpServletRequest request) {
System.out.println("1");
UserInfoDTO userInfoDTO = new UserInfoDTO();
userInfoDTO.setUserName("Akriti");
userInfoDTO.setUserId("1111");
String userString = new Gson().toJson(userInfoDTO);
//return new ModelAndView("index.jsp", "userString", userString);
System.out.println("user" + userString);
return userString;
}
And the js file I'm using:
'use strict';
(function () {
/**
* @ngdoc overview
* @name User Module
* @description Logged in User
*/
var user = angular.module('sol.user', []);
user.controller('sol.user.UserController', UserController);
alert("Hi");
// $state.go('home.user');
function UserController($scope, $http){
alert("Hello");
$http({method: 'GET',
url: '/user/details'})
.success(function(data) {
$scope.profiles = data;
})
.error(function(data) {
$scope.profiles = data;
alert( "failure");
alert( data);
});
$state.go('home.user');
}
})();
The url doesn't run at all as none of the system instructions are printed to the console. Please, help
+3
source to share
1 answer
Can you try the following code and open the Firebug network pane (or equivalent replacement) and check if the xhr network call was found on the url?
'use strict';
(function () {
/**
* @ngdoc overview
* @name User Module
* @description Logged in User
*/
var user = angular.module('sol.user', []).controller('UserController',['$scope',$http','$state',function(scope,http,state){
alert("Hello");
http({method: 'GET',
url: '/user/details'})
.success(function(data) {
scope.profiles = data;
})
.error(function(data) {
scope.profiles = data;
alert( "failure");
alert( data);
});
state.go('home.user');
}]);
})();
0
source to share