首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在java脚本数组中设置jsp变量?

如何在java脚本数组中设置jsp变量?
EN

Stack Overflow用户
提问于 2014-05-01 19:27:05
回答 3查看 1K关注 0票数 0

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

这是我的代码:

value.jsp

代码语言:javascript
复制
<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();
        }  

    %>

如何实现我想要的输出?

EN

回答 3

Stack Overflow用户

发布于 2014-05-01 19:43:41

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

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

此外,您正在制作10份JS。

代码语言:javascript
复制
jQuery(function ($) {
var roles = []; 
roles.push(<%= x%>)  
roles.push(<%= y%>) 

这个函数从来都不是封闭的。

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

票数 1
EN

Stack Overflow用户

发布于 2014-05-01 19:35:11

看起来,您正在清除角色变量var roles = [];,每次循环的传递都是这样。

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

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

票数 0
EN

Stack Overflow用户

发布于 2014-05-01 19:36:24

尝试用roles.push("<%= x%>")之类的引号将脚本封装起来,并检查数组初始化。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23415037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档