1

I have an javaScript function in my Thymeleaf page which has an array of countries

  var countries = ["Australia","Italy","France","Spain","India","China"];

But I want to pass that list of Countries from the controller method. So I passed an List of strings to the view via the model and accessed it in javaScript as follows,

Controller:

 List<String> list=new ArrayList<String>();
    list.add("Australia");
    list.add("Italy");
    list.add("France");
    list.add("Spain");
    list.add("India");
    list.add("China");

    model.addAttribute("list",list);

javaScript:

 var countries=[[${list}]];

But It doesn't work as expected.So please suggest a way to do this successfully

BenSV
  • 139
  • 4
  • 13
  • Shouldn't that be just normal array? var countries = list; incase if you are getting that from an API call it should be var countries = response.data – Yatin Gaikwad Mar 19 '20 at 07:16
  • 1
    duplicate : https://stackoverflow.com/a/25688291/11285149 – 1pulsif Mar 19 '20 at 07:19
  • 5
    Does this answer your question? [Setting up a JavaScript variable from Spring model by using Thymeleaf](https://stackoverflow.com/questions/25687816/setting-up-a-javascript-variable-from-spring-model-by-using-thymeleaf) – 1pulsif Mar 19 '20 at 07:20

0 Answers0