简体   繁体   English

如何在javascript数组中添加Java对象?

[英]how to add Java Object in javascript array?

in jsp file. 在jsp文件中。

    <% java.util.Vector <HighlightVO> conditions = bean.getPropVector("HighlightVOList");%>

    <script language="JavaScript">
    var conditions = []; 
        <% for(HighlightVO highlightVO : conditions){ %>
        conditions.push(<%=highlightVO%>); // not working.
        <%}%>

</script>

i am not able to add the highlighVO in conditions[]. 我无法在条件中添加highlighVO []。

Can any one help to do this operations. 任何人都可以帮助执行此操作。

The parameters of javascript push must be understanded by javascript. javascript必须了解javascript push的参数。 If you look at the javascript code generated, you will probably see something like : 如果您查看生成的javascript代码,则可能会看到类似以下内容的内容:

    conditions.push(HighlightVO@6d06d69c)

which is not understanded by javascript. javascript无法理解。

A solution is to implement the toString() method of HighlightVO in order to return the object in json format. 一种解决方案是实现HighlightVO的toString()方法,以便以json格式返回对象。

Adding toString method in HighlightVO class : 在HighlightVO类中添加toString方法:

  public String toString()
  {
    return '{'+
           'field1:'+field1+','+
           'field2:'+field2+','+
           ...
           '}';
  }

will generate : 将产生:

conditions.push({field1:1,
                 field2:'2', ...})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM