O
O
Orkhan Hasanli2019-03-17 03:48:26
Java
Orkhan Hasanli, 2019-03-17 03:48:26

How to pass parameter from properties in Spring Boot to js file?

Hello!
There is a need to pass some parameters from the properties file to the js file (js code is not inline, but a separate file and, accordingly, embedding with thymeleaf th: inline is not an option).
You need to pass the base url + application context to this js file.
For example
application.properties:

site.base.url=http://localhost:7777
server.servlet.context-path=/testApp

An excerpt from the js file:
$('#drag-and-drop-zone').dmUploader({ 
    url: 'backend/upload.php', // нужно провести здесь, замену без хардкодинга урла
    maxFileSize: 3000000, 
    onDragEnter: function(){
      this.addClass('active');
    }
}

Thanks in advance))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2021-03-03
@azerphoenix

I'll answer my own question.
If this is a classic MVC application and, for example, the thymeleaf template engine is used, then you can access the yml || properties in the following ways:
1)
${@environment.getProperty('css.specific.name')}
2) Either using annotation @ConfigurationProperties
and in html use:
<span th:text="@foo.getBar()"></span>
Useful related link:
https://stackoverflow.com/questions/56102116/acces...
PS Using Controller || ControllerAdvice for such purposes is not welcome.
Well, if you have a REST application, then this is configured on the client side inside the js framework.

E
Eugene, 2019-03-18
@zolt85

As the simplest option, you can add a simple controller on the server side, which will give you either the entire config in JSON format, or a specific value from it by name. I'm not good at writing plugins for jQuery, but I think it's possible to beat it through the plugin.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question