mc.suchecki
mc.suchecki

Reputation: 1898

Problems with including Bootstrap to SpringMVC project via WebJars

I've tried to use Bootstrap in my SpringMVC project: link to GitHub repo

Unfortunately, CSS files are not visible. The browser is throwing errors in the console like this:

GET http://localhost:8080/finager/webjars/jquery/2.0.3/jquery.js 404 (Not Found)

I think pom.xml has proper configuration, because I am able to view included WebJars (Bootstrap and JQuery) in Eclipse under Java Resources -> Libraries -> Maven Dependencies. I've also set path in servlet-context.xml like this (last line):

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/webjars/**" location="/META-INF/resources/webjars/"/>

I think this line is correct, because Tomcat no longer says that it don't know how to handle request starting with '/webjars/. All tutorials that I've found lists only these steps, and I couldn't find any useful information on the Internet. I've spent whole day on this, as I am new to Spring development , so any help would be hugely appreciated. Thanks in advance!

Upvotes: 3

Views: 9870

Answers (2)

Bassem Reda Zohdy
Bassem Reda Zohdy

Reputation: 12942

if you are using spring boot it is done automatically check the WebMvcAutoConfiguration class

https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
        "classpath:/META-INF/resources/", "classpath:/resources/",
        "classpath:/static/", "classpath:/public/" };

Upvotes: 0

Biju Kunjummen
Biju Kunjummen

Reputation: 49935

Specifying the location this way will fix the issue for you(note the classpath:) :

<resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

Also, you appear to have jquery 1.8.2 in your path, so the url should be along these lines:

http://localhost:8080/finager/webjars/jquery/1.8.2/jquery.js

Here is a good reference - https://spring.io/blog/2014/01/03/utilizing-webjars-in-spring-boot

Upvotes: 6

Related Questions