3

I've recently solved a problem of locating images in this community. You can look here.

However, one answer said:

Your app is a Spring Boot app. I think that you can also try to use the facilities provided by Spring Boot for serving static content. Anyway, you are doing it right now because you are using webjars for css and js libs!!! Be consistent with the tech that you are using.

This means, it's recommendable to locate the images files in /src/main/resources and not in /src/main/webapp.

How could i use the facilites that Spring Boot provides me?

Thanks

1 Answer 1

2

Spring Boot automatically configures production-ready settings and adds beans to your Spring project based on some dependencies that are declared in your maven/gradle build file. As long as you follow the conventions, then you do not have to do so much configuration -- "convention over configuration". For example, one convention is to load your images and static files in the resource classpath. Refer to the documentation: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/

Sign up to request clarification or add additional context in comments.

3 Comments

I've found that a convention to save static-content should be used in the path /src/main/resource/static/images. Then, I can use the sentence <img src="http://localhost:8080/images/myImage.png" />. However, it's a bad idea cause i can't ensure that my app will run at port 8080. How can I solve it?
You can use a relative path. For example if you are using thymeleaf for your view use <img th:src="@{/images/myImage.png}" />
th namespace is only available if Thymeleaf for Spring is enabled, but Thymeleaf is for dynamic content. For serving static content, see this and specially this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.