【问题标题】:How to set jsp variable in java script array?如何在javascript数组中设置jsp变量?
【发布时间】:2014-05-01 19:27:05
【问题描述】:

您好,我正在尝试将 jsp 变量设置为 javascript 数组。我试过了,但是当我运行我的代码时我无法做到这一点。我没有得到我的输出,我只看到一个空白屏幕。

这是我的代码:

value.jsp

<script>
             <%

       URL url;
         ArrayList<String> list1 = new ArrayList<String>();
            ArrayList<String> list2 = new ArrayList<String>();
           List commodity = null;
           List pric = null;
           int c = 0;
           int p = 0;

     try {
            // get URL content

            String a = "http://122.160.81.37:8080/mandic/commoditywise?c=paddy";
            url = new URL(a);
            URLConnection conn = url.openConnection();
            // open the stream and put it into BufferedReader
            BufferedReader br = new BufferedReader(
                               new InputStreamReader(conn.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String inputLine;
            while ((inputLine = br.readLine()) != null)
   {
                    System.out.println(inputLine);
                  //  sb.append(inputLine);
                   String s=inputLine.replace("|", "\n");
                   s = s.replace("~"," ");
                   StringTokenizer str = new StringTokenizer(s);
                    while(str.hasMoreTokens())

   {
       String mandi = str.nextElement().toString();
       String price = str.nextElement().toString();
       list1.add(mandi);
       list2.add(price);
   }

   }

       commodity = list1.subList(0,10);
                         pric = list2.subList(0,10);
                         for (c = 0,p = 0; c < commodity.size()&& p<pric.size(); c++,p++) 
            {
                String x = (String)commodity.get(c);
                String y = (String)pric.get(p);
                //out.println(y);
                //out.println(x);}
             %>



     jQuery(function ($) {
    var roles = []; 
     roles.push(<%= x%>)  
     roles.push(<%= y%>)  
     <%

            }
     %>

    var counter = 0;
    var $role = $('#role')
    //repeat the passed function at the specified interval - it is in milliseconds
    setInterval(function () {
        //display the role and increment the counter to point to next role
        $role.text(roles[counter++]);
        //if it is the last role in the array point back to the first item
        if (counter >= roles.length) {
            counter = 0;
        }
    }, 400)
    });

    </script>

            <%

     br.close();

            //System.out.println(sb);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }  

    %>

我怎样才能达到我想要的输出?

【问题讨论】:

    标签: java javascript jsp


    【解决方案1】:

    当你从 JSP 输出到 JS 时,你必须确保你的输出是有效的 JS。

    您当前的输出将输出一个不带引号的字符串,JS 解释器会将其视为 JS 代码,而不是您想要的字符串或数组。

    另外,你正在生产 10 个 JS 副本

    jQuery(function ($) {
    var roles = []; 
    roles.push(<%= x%>)  
    roles.push(<%= y%>) 
    

    并且该功能永远不会关闭。

    我会寻找一个不错的 JSON 输出库。这会大大简化你的生活。

    【讨论】:

    • JSON 基本上是一种人类可读的对象表示法,我会说像 XML。这是一个有用的 Java 库,可以帮助您进行实际实现,GSON
    【解决方案2】:

    看起来您在每次循环时都在清除角色变量 var roles = [];

    您可能需要考虑不发布整个文件,而是将示例与您需要帮助的部分配对。

    您还可以将数组放入 JSON 对象并在您的 javascript 中对其进行解析。这可能是一个更干净的实现。

    【讨论】:

    【解决方案3】:

    尝试用像 roles.push("") 这样的引号将 scriptlet 括起来,并检查数组初始化。

    【讨论】:

      猜你喜欢
      • 2015-01-26
      • 2013-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      • 2019-09-18
      • 1970-01-01
      相关资源
      最近更新 更多