Search code examples
javaangularjsjspstruts2struts2-json-plugin

how can we pass Struts2 action class to angularjs $http get


My goal is to display values as a list from a table to the model pop-up on the JSP page.

action

public class IcdAction extends ActionSupport {

    private List<Icd10> icdCodes;

    public String execute() throws MalformedURLException, JsonProcessingException {
        SomeProcess ap = new SomeProcess();
        icdCodes = ap.getList();
        return SUCCESS;     
    }
}

js

var myApp = angular.module('myApp', []);

myApp.controller('MainCtrl', function ($scope, $http) {

    $http.get('IcdCodesAction')
    .success(function(response){
         $scope.icdcodes = response;
    });
});

struts.xml

<action name="IcdCodesAction" class="com.emr.action.IcdAction" >    
    <result type="json">
    </result>
</action>

In the JSP file I am using ng-repeaton the icdcodes.

I am new to AngularJS. I am not able to figure out where is the issue which is preventing the list to be displayed.

If I use hardcoded json data for $scopes.icdcodes, then it is working fine.


Solution

  • The json result is processed by the struts2-json-plugin, that serializes the whole Action.

    Then you can:

    1. read only the desired object from the serialized action:

      $http.get('IcdCodesAction')
      .success(function(response){
           console.log("this is the serialzied action -> " + response);
           console.log("this is what I want:  " + response.icdCodes);
           $scope.icdcodes = response.icdCodes;
      });
      

      OR

    2. use the root attribute of the json result to specify the single element to serialize:

      <action name="IcdCodesAction" class="com.emr.action.IcdAction" >    
          <result type="json">
              <param name="root">icdCodes</param>
          </result>
      </action>