开发者

How to assign or initialize a Java array to Javascript array, without using JSP?

开发者 https://www.devze.com 2023-03-05 16:20 出处:网络
I am working in Play framework and I need to use Java array inside Javascript. I tried the followi开发者_C百科ng -

I am working in Play framework and I need to use Java array inside Javascript.

I tried the followi开发者_C百科ng -

var jsarray = ${javaArray};

Where javaArray is the array in the Java controller, being rendered to the HTML view.

But, it is not working. Can anyone please help me on this?


Don't forget you're not passing variables... You're writing javascript code

So, you have to make your array rendering nicely for Javascript

var jsArray = [%{javaArray.eachWithIndex{it,idx->
                 }%
                 ${it}${(idx<javaArray.size()-1)?",":""}
                 %{
                 }
                 }%]

Use something that fits with your template engine. You have to iterate through your array then prints out the values on the view

PS : I've used the Groovy notation...

0

精彩评论

暂无评论...
验证码 换一张
取 消