![](/img/trans.png)
[英]best way for javascript to access values from properties file in spring application
[英]In the Spring Framework for Java, what is the best way to store elements defined in the .properties file into a variable in JavaScript?
我目前有一個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
前五個似乎自動讀到我似乎找不到的地方。 最后四個,我想用Java語言訪問以存儲URL和布爾值。 我想用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";
現在,我正在從Javascript文件中讀取信息,該Javascript文件包含我最終想要擺脫的JSON對象。 它與application.properties相同,只是對最終用戶更可見。 如何獲得從application.properties文件到Javascript變量的鏈接?
您可以使用以下命令創建一個jsp文件:
<script>
var myLink1 = '<%=properties.getProperty("ui.firstLink")%>';
</script>
如果要使用spring,請創建PopertiesPlaceHolderConfigurer並在jsp中使用spring標記。 看這里
我不喜歡將JavaScript與服務器端語言混合使用:
因此,我將所需的變量表達式作為元標記或數據屬性,然后使用JS訪問它們。
<body data-uifirstlink='<%=properties.getProperty("uifirstLink")%>';
使用jQuery通過以下方式訪問: $('body').data('uifirstlink');
注意:理想情況下,這些數據屬性應盡可能地與上下文相關(而不是body
選擇更具體的位置)。
<meta name='uifirstlink' content='<%=properties.getProperty("uifirstLink")%>' />
用jQuery通過以下方式訪問: $('meta[name="uifirstlink"]').prop('content')
一種用於收回性質的服務是不是一件好事,因為:
最后,您可以考慮使用Cookie或標頭,但我認為這不是那么簡單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.