0

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:

    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. I'd like to write something in JS like:

    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. It was all the same information as application.properties, except it is more visible to the end user. How do I get the links from my application.properties file into Javascript variables?

  • Have a Spring MVC web Service expose the properties you wish to share and implement a web Service call from your JS Client. – Daniel Lavoie Aug 25 '16 at 15:40

2 Answers2

0

You could create a jsp file with this:

<script>
    var myLink1 = '<%=properties.getProperty("ui.firstLink")%>';
</script>

If you want to use spring, create a PopertiesPlaceHolderConfigurer and use spring tags in jsp. See here

Community
  • 1
  • 1
6ton
  • 4,174
  • 1
  • 22
  • 37
0

I don't like to mix JavaScript with the server-side language as it:

  • makes life harder for editors
  • is harder to read
  • couples JS with the server-side technology

Therefore, I would put the desired variable expressions as meta tags or data-attributes and then access them using JS.

  • <body data-uifirstlink='<%=properties.getProperty("uifirstLink")%>'; accessed with jQuery by: $('body').data('uifirstlink');

    note: ideally, these data attributes should be as contextual as possible (instead of body choose a more specific place).

  • <meta name='uifirstlink' content='<%=properties.getProperty("uifirstLink")%>' /> accessed with jQuery by: $('meta[name="uifirstlink"]').prop('content')

A service for retrieving properties is not good because:

  • You need to wait for the page to load to run the call and use the variables
  • You have unnecessary Ajax calls
  • You create more coupling between client and server

Finally, you could consider cookies or headers, but I don't think is so straightforward.

Luís Soares
  • 5,726
  • 4
  • 39
  • 66