0

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.

K. G
  • 31
  • 1
  • 7
  • did you try this answer https://stackoverflow.com/a/15796409/3892213 – best wishes Feb 03 '18 at 08:53
  • Thank you for the reply. I tried this and it worked. new ClassPathResource("../../resources/static/img/logo.jpg"); However, I am not sure if it is the right way to use double dots to access the resource. – K. G Feb 03 '18 at 10:41

0 Answers0