spring mvc 3 populate javascript var from property file

*爱你&永不变心* 提交于 2020-01-05 09:28:21

问题


I need to populate a JavaScript variable with a property value (that defined in a property file) when page get loaded . i am using spring mvc 3. is there any best way to do it? appreciate if someone give me some clue.

Thanks in advance.


回答1:


  • Create an initializer method in the javascript file
  • output the properties in the page that includes the js. so that they form a valid javascript structure (array, object, whatever)
  • pass the structure to the initializer.

The 1st step may look like (in the .js file):

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

The 2nd step may look like (in your 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);



来源:https://stackoverflow.com/questions/5595868/spring-mvc-3-populate-javascript-var-from-property-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!