How to transfer java array to javaScript array using jsp?

后端 未结 6 750
闹比i
闹比i 2020-12-06 01:51

I have a list of strings on my server which I am trying to get to the client in the form of an array. The code I am attempting to use is the following:

Within the js

6条回答
  •  -上瘾入骨i
    2020-12-06 02:39

    For me this solution has worked. First of all You should make a JSONArray and use it's toJSONString() method. This method converts the list to JSON text. The result of it is a JSON array.

    <% 
    List exampleList = new ArrayList<>();
    exampleList.add("Apple");
    exampleList.add("Orange");
    exampleList.add("Lemon");
    
    JSONArray fruitList = new JSONArray();
    fruitList.addAll(exampleList);
    %>
    

    In your JSP page you should invoke the toJSONString() method of the list, and pass the JSON text to a JavaScript array.

    
    

    (Optionally You could make a simple getter method for the list. In case if you only instantiate the JAVA class - which has the list field - int the JSP page.)

提交回复
热议问题