Spring mvc 3 populate javascript var from properties file

I need to populate a JavaScript variable with the property value (which is defined in the properties file) when the page loads. I am using spring mvc 3. Is there a better way to do this? appreciate if anyone gives me a hint.

Thanks in advance.

+1
source share
1 answer
  • Create an initialization method in javascript file
  • display properties on the page containing js. so that they form a valid javascript structure (array, object, whatever)
  • pass the structure to the initializer.

The first step may look (in the .js file):

var options; function init(initOptions) { options = initOptions; } 

The second step may look like this: (on the jsp page):

 var a = new Array(); <c:forEach items="${properties}" var="entry"> a.push({key: '${entry.key}', value: '${entry.value}'}); </c:forEach> 

And finally, init(a);

+1
source

Source: https://habr.com/ru/post/1346809/


All Articles