我有一个 Spring项目,我将以下webjars包含在pom.xml中: dependency groupIdorg.webjars/groupId artifactIdbootstrap/artifactId version3.3.7-1/version/dependencydependency groupIdorg.webjars/groupId artifactIdjquery/artifactId vers
<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>
然后我在HTML视图中包含以下链接和脚本:
<link rel="stylesheet" href="@{/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}" /> <script src="@{/webjars/jquery/3.1.1/jquery.min.js}"></script> <script src="@{/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script>
但它没有用,没有找到映射:
[org.springframework.web.servlet.PageNotFound] (default task-15) No mapping found for HTTP request with URI [/TestPublicWeb-0.0.1-SNAPSHOT/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css] in DispatcherServlet with name 'testapp'
…所以我尝试将以下映射包含到servlet.xml中:
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
但是有了这个,找不到我/ TestApplication的映射:
[org.springframework.web.servlet.PageNotFound] (default task-13) No mapping found for HTTP request with URI [/TestApplication/] in DispatcherServlet with name 'testapp'
webjars应该如何以正确的方式包含在Spring项目中?
问题是您将标准HTML href标记与Thymeleaf的语法@ {}混合.更改如下:<link rel="stylesheet" th:href="@{/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}" /> <script th:src="@{/webjars/jquery/3.1.1/jquery.min.js}"></script> <script th:src="@{/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script>
如果您使用的是Spring Security,还需要在configure(HttpSecurity http)方法中指定对webjars的授权,例如:
http.authorizeRequests().antMatchers("/webjars/**").permitAll();