Issue
I have a Maven webapp with Spring Boot and JSP. I'm trying to import webjars to use bootstrap on my JSP, I add the dependencies on my pom (locator included) and I put the resource handler on my webconfig:
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7-1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator</artifactId>
<version>0.30</version>
</dependency>
Now I add the references on my webconfig:
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/webjars/**")
.addResourceLocations("/webjars/");
}
}
And then I try to import the jars in my JSP:
<script src="/webjars/jquery/3.1.1/jquery.min.js"></script>
But it doesn't work. I put css inside webapp folder but I cannot link it inside my JSP.
How can I solve this? What's wrong?
PS: I have my JSP in web-inf/jsp/pages
and I have added a context path to application.properties
(but I think there are some conflicts with the WebConfig
class).
Solution
By using @EnableWebMvc
, you have indicated to Spring Boot that you want to take complete control of configuring Spring MVC. That means that all of its auto-configuration of static resources, web jars support, etc has been switched off.
You can delete your WebConfig
class entirely as it is duplicating configuration that Spring Boot will do for you automatically. If you need to customise the default configuration, you should implement WebMvcConfigurer
as you have done, but omit the @EnableWebMvc
annotation.
Answered By - Andy Wilkinson
Answer Checked By - Gilberto Lyons (JavaFixing Admin)