通过WebJars包含Bootstrap到SpringMVC项目的问题

mc.*_*cki 3 java eclipse spring-mvc maven twitter-bootstrap

我试图在SpringMVC项目中使用Bootstrap: 链接到GitHub repo

不幸的是,CSS文件不可见.浏览器在控制台中抛出错误,如下所示:

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

我认为pom.xml具有正确的配置,因为我能够在Java资源 - >库 - > Maven依赖项下查看Eclipse中包含的WebJars(Bootstrap和JQuery).我也在servlet-context.xml中设置了这样的路径(最后一行):

<!-- 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/"/>
Run Code Online (Sandbox Code Playgroud)

我认为这一行是正确的,因为Tomcat不再说它不知道如何处理以'/ webjars /开头的请求.我找到的所有教程都只列出了这些步骤,我在互联网上找不到任何有用的信息.我花了一整天的时间,因为我是Spring开发的新手,所以任何帮助都会非常感激.提前致谢!

Bij*_*men 6

以这种方式指定位置将为您解决问题(请注意classpath:):

<resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
Run Code Online (Sandbox Code Playgroud)

此外,您的路径中似乎有jquery 1.8.2,因此URL应该沿着这些行:

http://localhost:8080/finager/webjars/jquery/1.8.2/jquery.js
Run Code Online (Sandbox Code Playgroud)

这是一个很好的参考 - https://spring.io/blog/2014/01/03/utilizing-webjars-in-spring-boot