user3534483
user3534483

Reputation: 2363

Serving static contents in Spring Boot

I am developing a new poc for web application from Spring Boot. The packaging type of my application in war. In this all i want is to display some contents on a jsp. For that i have created a small jsp, and requierd css/images/js files i have put in resources/static folder. So my static folder contains css/images/js folders. I've added following code in my configuration file. My configuration extends from WebMvcConfigurerAdapter

public void addResourceHandlers(ResourceHandlerRegistry registry) {
    String[] pathPatterns = {"/components/**", "/images/**", "/scripts/**", "/styles/**"};
    String[] resourceLocations = {"classpath:/static/components/", "classpath:/static/images/", "classpath:/static/scripts/, classpath:/static/styles/"};
    registry.addResourceHandler(pathPatterns).addResourceLocations(resourceLocations);
}

However, my jsp does not get the reference of these file.

JSP Code

How to solve above problem..

Second concern, as per the spring boot reference documentation, it serves the static content which are located in static folder. that means i should be able to access files from my static folder directly in below way

http://localhost:8080/styles/main.css

But this is also not working

Third Issue - static contents are served by default servlet ..is this true that Default servlet in enabled by default in Spring Boot application

Please Help

Upvotes: 0

Views: 715

Answers (1)

Sanjay
Sanjay

Reputation: 8955

Putting the static resources inside src/main/resources/static folder works for me without any addResourceHandlers configuration. For example, I have a css file at

src/main/resources/static/public/css/styles.css

which I refer from my JSP like this:

<link href="/public/css/styles.css" rel="stylesheet">

You should have put your JSPs inside src/main/webapp/WEB-INF, and set the packaging to war rather than jar, due to the limitations of having JSPs in Spring Boot.

Upvotes: 1

Related Questions