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);
}
}
>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}