I have a Spring Boot application with the following structure:
src
|--main
---|--java
---|--resources
------|--templates
------|--application.properties
---|--webapp
------|--resources
---------|--static
------------|--css
------------|--img
---------------|--logo.jpg
------------|--js
------------|--favicon.ico
------|--WEB-INF
---------|--view
------------|--index.jsp
In my MvcConfig, I have added ResourceHandler as below
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
In my JSPs, I can access the images from img folder using
<img src="<c:url value="/resources/static/img/logo.jpg" />">
Trying to access the same image in a class, I tried the following code:
ClassPathResource cpr = new ClassPathResource("logo.jpg");
and also tried using many variations for the image path. However, every time I receive a false value for cpr.exists()
Surprisingly, I received true when I changed the parameter to favicon.ico.
What am I doing wrong? Please explain this behaviour and also explain what is the right way to access the resource.