java - 如何在javascript数组中设置jsp变量?

标签 java javascript jsp

嗨,我正在尝试将 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();
        }  

    %>

如何实现我想要的输出?

最佳答案

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

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

此外,您还制作了 10 份 JS

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

并且该函数永远不会关闭。

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

关于java - 如何在javascript数组中设置jsp变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23415037/

相关文章:

java - Olingo4 向后兼容吗?

java - 如何使用 gradle 构建 java hidl 客户端

javascript - 使用 JS 或 JQuery,使用 jQuery 的 listselect.js 将选择中的所有内容从一个列表移动到 "selected"

java - 通过 Java 从 Javascript 更改填充多个 HTML 语句

java - 增加字符串内的数字

javascript - 如何根据行 ID 列表更改表中行的顺序?

javascript - 该脚本一次后不会更改图像

java - 从 Servlet 的 HTML 部分的文本框中获取值

java - NumberFormatException 使用 <c :url in jsp

java - 在 AndroidManifest 中注册的 BroadcastReceiver 不工作