[英]In the Spring Framework for Java, what is the best way to store elements defined in the .properties file into a variable in JavaScript?
I currently have a Java Spring project where the bulk of the front is in AngularJS, HTML, etc. I currently have an application.properties file that holds: 我目前有一个Java Spring项目,其中大部分是AngularJS,HTML等。我目前有一个application.properties文件,其中包含:
name:myName
idNum:8888888888
password:squirrels
contextPath:/ilovesquirrels-ui
server:0000
ui.firstLink: www.google.com
ui.secondLink: www.yahoo.com
ui.thirdLink: www.w3schools.com
myBool: False
The first five seem to read in automatically to a place I cannot seem to find. 前五个似乎自动读到我似乎找不到的地方。 The last four, I'd like to access in Javascript to store the urls and the boolean.
最后四个,我想用Java语言访问以存储URL和布尔值。 I'd like to write something in JS like:
我想用JS写一些东西:
var myLink1 = "something that accesses ui.firstLink in application.properties";
var myLink2 = "something that accesses ui.secondLink in application.properties";
var myLink3 = "something that accesses ui.thirdLink in application.properties";
Right now, I am reading information from a Javascript file that holds a JSON object that I'd eventually like to get rid of. 现在,我正在从Javascript文件中读取信息,该Javascript文件包含我最终想要摆脱的JSON对象。 It was all the same information as application.properties, except it is more visible to the end user.
它与application.properties相同,只是对最终用户更可见。 How do I get the links from my application.properties file into Javascript variables?
如何获得从application.properties文件到Javascript变量的链接?
You could create a jsp file with this: 您可以使用以下命令创建一个jsp文件:
<script>
var myLink1 = '<%=properties.getProperty("ui.firstLink")%>';
</script>
If you want to use spring, create a PopertiesPlaceHolderConfigurer and use spring tags in jsp. 如果要使用spring,请创建PopertiesPlaceHolderConfigurer并在jsp中使用spring标记。 See here
看这里
I don't like to mix JavaScript with the server-side language as it: 我不喜欢将JavaScript与服务器端语言混合使用:
Therefore, I would put the desired variable expressions as meta tags or data-attributes and then access them using JS. 因此,我将所需的变量表达式作为元标记或数据属性,然后使用JS访问它们。
<body data-uifirstlink='<%=properties.getProperty("uifirstLink")%>';
accessed with jQuery by: $('body').data('uifirstlink');
使用jQuery通过以下方式访问:
$('body').data('uifirstlink');
note: ideally, these data attributes should be as contextual as possible (instead of body
choose a more specific place). 注意:理想情况下,这些数据属性应尽可能地与上下文相关(而不是
body
选择更具体的位置)。
<meta name='uifirstlink' content='<%=properties.getProperty("uifirstLink")%>' />
accessed with jQuery by: $('meta[name="uifirstlink"]').prop('content')
<meta name='uifirstlink' content='<%=properties.getProperty("uifirstLink")%>' />
用jQuery通过以下方式访问: $('meta[name="uifirstlink"]').prop('content')
A service for retrieving properties is not good because: 一种用于收回性质的服务是不是一件好事,因为:
Finally, you could consider cookies or headers, but I don't think is so straightforward. 最后,您可以考虑使用Cookie或标头,但我认为这不是那么简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.