我们如何将 Struts2 操作 class 传递给 angularjs $http get

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

我的目标是将 table 中的值作为列表显示到 JSP 页面上的模型弹出窗口。

动作

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>

在 JSP 文件中,我在 icdcodes 上使用 ng-repeat

我是 AngularJS 的新手。我无法弄清楚阻止列表显示的问题在哪里。

如果我使用 $scopes.icdcodes 的硬编码 json 数据,那么它工作正常。

json结果由struts2-json-plugin处理,即serializes the whole Action.

那么您可以:

  1. 只从序列化操作中读取所需的对象:

    $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;
    });
    

  2. 使用 json 结果的 root 属性指定要序列化的单个元素:

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