1

In servlet I got something like:

Map<String, String> myFruits = giveMeSomeFruits();
request.serAttribute("myFruits", myFruits);

And in .js I got:

<label ng-repeat='(key, value) in myFruits'>
    <input type='checkbox' value='{{value}}' ng-click='eat(value)'/> {{key}}
</lable>

Now I need to pass the map myFruits from servlet to angularJs, is there any clean way to do that?

Du Sun
  • 63
  • 1
  • 5

2 Answers2

0

This might work.

1.Yes have an ajax call to the servlet from angular.
2.Convert the map to Json in Java using JSONObject and send the response from servlet as JSON.

return new JSONObject(myFruits);

 $.ajax(
     url : url,   // Controller URL
     success: function(resultJSON){
          //Assign resultJSON to myFruits variable
          $scope.myFruits = resultJSON;
      }});
Community
  • 1
  • 1
0

In your controller, define a function called getMap maybe. Make a call to the servlet using your url for the same, using Method 'GET'.

$scope.getMap = function () {
    $http({method: 'GET', url: 'your url'}).
        success(function (data, status, headers, config) {
            console.log(data);
            $scope.myFruits = {data: data};
        }).
        error(function (data, status, headers, config) {
            console.log("error");
        });
};

Now you have to override the doGet() function in your servlet, which retrieves this map for you through a query to your database. Convert the map to Json using

response = gsonHelper.toJson(yourMap);

Send this json as response back to your controller and assign it to a variable defined in scope, say fruits.

Now you can use fruits in your markup to iterate through the map.