首页 > 解决方案 > Sprinboot webapp doesn't load webjars and css

问题描述

I have maven webapp with springboot 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 than I try to import the jars in my jsp:

<script src="/webjars/jquery/3.1.1/jquery.min.js"></script>

But doesn't work.. I put css inside webapp folder but I cannot link it inside my jsp..

How can I solve this?? Whats wrong?? Thank you all

Ps: I have my jsp in web-inf/jsp/pages and I have add a context path to application.properties (but I think there are some conflict with WebConfig class)

标签: jspspring-bootweb-applicationswebjars

解决方案


add the resourceChain and set it to false like this :

 registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/").resourceChain(false);

This is apparently not a webjar-locator problem.call to resourceChain() method should be necessary for configuration of webjar-locator.


推荐阅读