jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 如何将Struts2动作类中的InputStream值传递给jsp页面中的ajax并将值转换为Json数组大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将json数组从Struts2动作类传递给jsp页面.我正在尝试将数据集作为字符串发送.我想知道的是,我怎样才能在 javascript中读取这些数据.

这是我在动作类中的方法

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>

jsp中的javascript函数

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

解决方法

因为您使用流结果类型从服务器返回字符串化版本的JSON(注意,该流结果类型可能不合适,请参见下文),您需要使用 JSON.parse()将其解析为JSON,如果您使用jQuery,请更好地使用 $.each

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个可用于序列化为json的jar,或者如果你使用的是struts2-json-plugin,那么它有内置的序列化器.如果您使用的是struts2-rest-plugin,那么您可以使用其他序列化程序,如Jackson.您选择库来序列化数据的方式超出了本答案的范围.你可以在SO和Struts site上找到很多例子.他们中的大多数使用json插件返回浏览器支持的JSON对象,即不需要解析,但是解析JSON对于避免错误和数据丢失很有用.

大佬总结

以上是大佬教程为你收集整理的jquery – 如何将Struts2动作类中的InputStream值传递给jsp页面中的ajax并将值转换为Json数组全部内容,希望文章能够帮你解决jquery – 如何将Struts2动作类中的InputStream值传递给jsp页面中的ajax并将值转换为Json数组所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。