0

我的 Web 应用程序中有一个 jsp。此 jsp 在网格中显示来自 json 的数据。:

            <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
            <title>ARGO</title>
            <link rel="stylesheet" type="text/css" href="css/jquery-ui.css" />
            <link rel="stylesheet" type="text/css" href="jtable/themes/metro/blue/jtable.min.css"/>
            <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
            <script type="text/javascript" src="js/jquery-ui.js"></script>
            <script type="text/javascript" src="jtable/jquery.jtable.min.js"></script>
            <script type="text/javascript" src="js/argo.js"></script>
<body><div id= "ProductTable"></div>
                    $('#ProductTable').jtable({
                        title: 'Table of people',
                        actions: {
                           listAction: '/bsnet/test.html'
                         },

                        fields: {
                            productid: {
                                title: "Organization",
                                width:"20%"
                            },
                            productname: {
                                title: "Role",
                                width: "10%"
                            },
                            unitcost: {
                                title: "Status",
                                width: "20%"
                            }
                        }
                    });
                    $('#ProductTable').jtable('load');
                });
            </script>
            </html>   

我正在使用 jtable 在网格中显示内容。
JTable 只需要 JSON 响应。它期望以这种格式响应:

 {  
             "Result":"OK",
             "Records":[
                {"productid":"FI-SW-01","productname":"Koi","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"}}]}

我的 Web 服务没有发送 "Result":"OK" 作为 json 响应的一部分。

所以我决定在中间嵌入一个 HTML,它将把它添加到响应中并将它发送到 jtable jsp。

            This is the HTML that I created:

                <html>
                    <head>
                        <script type="text/javascript" src="js/jquery-1.9.1.js"></script>
                    </head>
                    <body>
               <!-- placing this in the body of the HTML  -->       
        {"Result":"OK","Records": 
                        <script type="text/javascript">
                        $(document).ready(function(){
    // json file that contains the server response
    // this will be replaced by a web service URL                     
    $.getJSON("/test/data.json", function(data){
    // adding the response to the body
                            $('body').append("<span>"+JSON.stringify(data)+"</span>}");
                          });
                        });
                        </script>
                    </body>
                </html>
             When I refer to the html in the jsp by adding it as part of the URL, I get the entire HTML content like: <html><head>....
            but I want only the json response or the body content 

            {
             "Result":"OK",
             "Records":[
                {"productid":"FI-SW-01","productname":"Koi","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"}]}

来自jsp中的HTML。那可能吗?
当我在浏览器中点击 HTML 时,我可以看到所需的响应。
有什么帮助???

4

1 回答 1

1

好的,这是一个使用 JSP scriptlet 的最简单的解决方案:

将其放入您的包装 JSP(仅此):

<%@ page import="java.net.URL" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.util.Scanner" %>
{
"Result":"OK",
"Records":
<%
    final String yourServiceURL = "your_FULL_Url_with_http_and_stuff";
    final String yourServiceEncoding = "UTF-8";

    final InputStream stream = new URL(yourServiceURL).openStream();
    final String text = new Scanner(stream, yourServiceEncoding ).useDelimiter("\\A").next();
    out.write(text);
%>
}

<%
    response.setContentType("application/json");
%>

如果从流中读取失败,您还可以向该代码添加一些异常处理以生成“结果:错误”。

于 2013-04-19T07:10:03.320 回答