0

controller code

@RequestMapping(value="/welcomes", method = RequestMethod.GET, produces="MediaType.application/json")
    public @ResponseBody ModelAndView welcome(@ModelAttribute UserBean userBean)
    {
        List<UserBean> usernames=new ArrayList<UserBean>();
        usernames = retrievedataservice.findAllUsers(userBean);
        System.out.println(usernames.size());
        return new ModelAndView("welcomes", "usernames", usernames);
    }

angular js code

<script>
var app = angular.module('myApp', []);
app.controller('UserController', function($scope, $http, $location){
    $scope.usernames=[];
        var url = $location.absUrl() + "http://localhost:8080/SpringAngular/welcomes";
        $http.get(url).then(function (response) 
        {
            $scope.usernames = response.records;
        },function error(response) 
        {
            $scope.postResultMessage = "Error with status: " +  response.statusText;
        });
});
</script>
<div data-ng-app="myApp" data-ng-controller="UserController">
   <table border="1" width="50%" height="50%"> 
    <tr><th>user_name</th><th>phone</th><th>email</th></tr>
     <tr data-ng-repeat="user in usernames">
     <td>{{user.username}}</td>
      <td>{{user.phone}}</td>
       <td>{{user.email}}</td>
       </tr>   
   </table>   

Actually, the controller returns the list of data, but angular js cannot get the data. It returns the null values. it cannot integrate with angular js and controller.

1
  • var url = $location.absUrl() + "http://localhost:8080/SpringAngular/welcomes"; does not look right. Commented Dec 3, 2018 at 12:10

2 Answers 2

1

Your controller is not doing what you are expecting: annotating the method with @ResponseBody will wrap the return value of your method in the response body, also produces attribute is wrong. try something like this:

@RequestMapping(value="/welcomes", method = RequestMethod.GET, produces="application/json")
    public @ResponseBody Object welcome(@ModelAttribute UserBean userBean)
    {
        List<UserBean> usernames=new ArrayList<UserBean>();
        usernames = retrievedataservice.findAllUsers(userBean);
        System.out.println(usernames.size());
        return usernames; // you can also do ResponseEntity.ok().body(usernames)
    }
Sign up to request clarification or add additional context in comments.

1 Comment

compile time error : your method return type is still ModelAndView but you are returning List<UserBean> which is contradictory
1

This will not work as you annotated method as @ResponseBody so it will send response in the body but you are returning modelandview which obviously will not work.

Angular doesn't have to do anything about modelandview but it is expecting json reponse so it will map.

So change the method like below by sending List<UserBean> in response.

@RequestMapping(value="/welcomes", method = RequestMethod.GET, produces="MediaType.application/json")
    public @ResponseBody List<UserBean> welcome(UserBean userBean)
    {
        List<UserBean> usernames=new ArrayList<UserBean>();
        usernames = retrievedataservice.findAllUsers(userBean);
        System.out.println(usernames.size());
        return usernames;
    }

Above change is enough to return response properly to calling environment but you need to make sure that you hit the method properly.

As suggested by @Dmitry var url = $location.absUrl() + "http://localhost:8080/SpringAngular/welcomes"; might not be right.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.