如何将Struts 2 操作class 中的InputStream 值传递给JSP 页面中的Ajax 并将该值转换为JSON 数组

How to pass InputStream value in Struts 2 action class to Ajax in JSP page and convert the value into JSON Array

我想将 JEON 数组从 Struts 2 操作 class 传递到 JSP 页面。我正在尝试将数据集作为字符串发送。我想知道的是,如何读取 JavaScript.

中的那些数据

这是我在Action中的方法 class:

private InputStream inputStream;

/* getter and setter*/

public String getClientMilestone() throws DAOTransientException, DBConfigException{
        PaymentScheduleDao paymentScheduleDao = new PaymentScheduleDao();
        List <PaymentMilestone> paymentScheduleInfo = paymentScheduleDao.getClientMilestoneInfo(projectId);
        String result = "[";

        for(int i=0; i<paymentScheduleInfo.size(); i++){
            
            result += "{"+"'"+"item"+i+"' : {"+ "'"+"milestoneName"+ "'"+":"+"'"+paymentScheduleInfo.get(i).getMilestone_name()+"'"+"}"+"},";
            
        }
        result += "]";
        System.out.println("result is "+result);
        inputStream = new StringBufferInputStream(result);
        return "success";
    }

打印如下:

result is [{'item0' : {'milestoneName':'milestone 1'}},{'item1' : {'milestoneName':'milestone 2'}}]

struts.xml:

<package name="ClientMilestone" namespace="/" extends="struts-default">
        <action name="getClientMilestone" class="packageName.PaymentScheduleAction" method="getClientMilestone">
            <result name="success" type="stream">
            <param name="contentType">text/html</param>
            <param name="inputName">inputStream</param>
            </result>
            <result name="failure">./LandingPage.jsp</result>
            <result name="error">./Error.jsp</result>
        </action>
    </package>

JavaScript 中的函数 JSP:

function createOrViewPs() {
    
    var projectId = document.getElementById("projectId").value;
    $.ajax({ 
        method: "GET",
        url: "getClientMilestone",
        data: {"projectId" : projectId},
        traditional: true, 
        success:
            function(result){
                var jsonArr = result;
            
                for (var i=0; i<jsonArr.length; i++)
                    for (var name in jsonArr[i]) {
                        alert("Item name: "+name);
                        alert("Source: "+jsonArr[i][name].milestoneName);
                }
            },
        error: 
            function(){
                alert("fail");
            }
    });         
} 

因为你 return 来自服务器的 JSON 的字符串化版本 stream 结果类型(注意,流结果类型可能不合适,见下文),你需要用 JSON.parse() and if you are using jQuery better use $.each

将其解析为 JSON
var jsonArr = JSON.parse(result);
$.each (jsonArr, function(index, value){
  $.each (value, function(key, value){
    console.log("Item name: "+key);
    console.log("Source: "+value.milestoneName);
  });
});

您做错的是手动构建 json。您应该使用将 Java 对象序列化为 JSON 的工具。 Struts2 有 json-lib available jar in the package that can be used to serialize to json, or if you are using struts2-json-plugin then it has built-in serializer. if you are using struts2-rest-plugin then you can use other serializer like Jackson. The way you choose the library to serialize your data is out of the scope of this answer. You can find many examples on SO and on Struts site。他们中的大多数使用 json 插件 returns JSON 浏览器支持的对象,即不需要解析,但是解析 JSON 有助于避免错误和数据丢失。