Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a better way to pass a variable to the view layer instead of using controller?

Each time I create a controller route then I must pass a variable to the view layer :

@Configuration
public class Config {

    @Value("${api.url}")
    private String apiUrl;

    @Bean
    public String API_URL() {
        return apiUrl;
    }

}

@Controller
public class ClientController {

    @Autowired
    private String API_URL;
        
    @GetMapping("/clients")
    public String viewListe(Model model) {
        model.addAttribute("API_URL", API_URL); // here is the passing of the variable to the view layer
        return "client/client-list";
    }
}

It is tedius. So I want to know if there is a better way to pass this variable ; for example is it possible to pass it in the @SpringBootApplication part ?

@SpringBootApplication
public class BackendApplication {
    public static void main(String[] args) {
        SpringApplication.run(BackendApplication.class, args);
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It’s possible to access a bean inside your Thymeleaf views. So in your case instead of passing the API_URL to every model as attribute just access the string like this: ${@API_URL}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading