<i id='ecUlM'><tr id='ecUlM'><dt id='ecUlM'><q id='ecUlM'><span id='ecUlM'><b id='ecUlM'><form id='ecUlM'><ins id='ecUlM'></ins><ul id='ecUlM'></ul><sub id='ecUlM'></sub></form><legend id='ecUlM'></legend><bdo id='ecUlM'><pre id='ecUlM'><center id='ecUlM'></center></pre></bdo></b><th id='ecUlM'></th></span></q></dt></tr></i><div id='ecUlM'><tfoot id='ecUlM'></tfoot><dl id='ecUlM'><fieldset id='ecUlM'></fieldset></dl></div>

  1. <tfoot id='ecUlM'></tfoot>
    <legend id='ecUlM'><style id='ecUlM'><dir id='ecUlM'><q id='ecUlM'></q></dir></style></legend>

      <bdo id='ecUlM'></bdo><ul id='ecUlM'></ul>

  2. <small id='ecUlM'></small><noframes id='ecUlM'>

      如何将 Struts 2 动作类中的 InputStream 值传递给 JSP 页面中的 Ajax 并将该值转换为 JS

      时间:2023-09-24

        <tfoot id='2mb12'></tfoot>
          <bdo id='2mb12'></bdo><ul id='2mb12'></ul>
            <i id='2mb12'><tr id='2mb12'><dt id='2mb12'><q id='2mb12'><span id='2mb12'><b id='2mb12'><form id='2mb12'><ins id='2mb12'></ins><ul id='2mb12'></ul><sub id='2mb12'></sub></form><legend id='2mb12'></legend><bdo id='2mb12'><pre id='2mb12'><center id='2mb12'></center></pre></bdo></b><th id='2mb12'></th></span></q></dt></tr></i><div id='2mb12'><tfoot id='2mb12'></tfoot><dl id='2mb12'><fieldset id='2mb12'></fieldset></dl></div>
            • <legend id='2mb12'><style id='2mb12'><dir id='2mb12'><q id='2mb12'></q></dir></style></legend>

              <small id='2mb12'></small><noframes id='2mb12'>

                  <tbody id='2mb12'></tbody>

                本文介绍了如何将 Struts 2 动作类中的 InputStream 值传递给 JSP 页面中的 Ajax 并将该值转换为 JSON 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                I want to pass JEON array from Struts 2 action class to JSP page. I'm trying to send the data set as a string. The thing I want to know is, How can I read those data in JavaScript.

                这是我在 Action 类中的方法:

                This is my method in 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>
                

                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 的字符串化版本,结果类型为 stream(注意,流结果类型可能不是合适,见下文),您需要使用 JSON.parse() 如果你使用 jQuery,最好使用 $.each

                Because you return a stringified version of JSON from the server with the stream result type (Note, that stream result type might not be appropriate, see below), you need to parse it to JSON with JSON.parse() and if you are using jQuery better use $.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 可用的jar,可以用来序列化为json,或者如果您正在使用 struts2-json-plugin 然后它有内置的序列化器.如果您使用的是 struts2-rest-plugin那么您可以使用其他序列化程序,例如 Jackson.您选择库来序列化数据的方式超出了此答案的范围.您可以在 SO 和 Struts 站点 上找到许多示例.他们中的大多数使用返回浏览器支持的 JSON 对象的 json 插件,即不需要解析,但是解析 JSON 有助于避免错误和数据丢失.


                What you did wrong is building json manually. You should use a tool that serializes Java object to JSON. Struts2 has 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. Most of them using json plugin that returns JSON object that supports by the browser, i.e. doesn't need parsing, however parsing for JSON is useful to avoid errors and data lose.

                这篇关于如何将 Struts 2 动作类中的 InputStream 值传递给 JSP 页面中的 Ajax 并将该值转换为 JSON 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Struts2 国际化使用数据库 下一篇:如何使用 blueimp jQuery 文件上传:Struts 2 中的空文件上传结果,文件项为空

                相关文章

                1. <small id='peQur'></small><noframes id='peQur'>

                  1. <legend id='peQur'><style id='peQur'><dir id='peQur'><q id='peQur'></q></dir></style></legend>

                    • <bdo id='peQur'></bdo><ul id='peQur'></ul>
                    <i id='peQur'><tr id='peQur'><dt id='peQur'><q id='peQur'><span id='peQur'><b id='peQur'><form id='peQur'><ins id='peQur'></ins><ul id='peQur'></ul><sub id='peQur'></sub></form><legend id='peQur'></legend><bdo id='peQur'><pre id='peQur'><center id='peQur'></center></pre></bdo></b><th id='peQur'></th></span></q></dt></tr></i><div id='peQur'><tfoot id='peQur'></tfoot><dl id='peQur'><fieldset id='peQur'></fieldset></dl></div>
                    <tfoot id='peQur'></tfoot>