2017-01-09 88 views
3

我,我列入以下webjars到pom.xml彈簧項目:Spring MVC的使用Thymeleaf +引導webjars

 <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映射未發現:包括在sprinf項目以正確的方式

[org.springframework.web.servlet.PageNotFound] (default task-13) No mapping found for HTTP request with URI [/TestApplication/] in DispatcherServlet with name 'testapp' 

如何應該是webjars?

回答

1

我可以想到一些事情,爲什麼上述不適合你。確保你的servlet.xml文件中使用了spring mvc命名空間。看起來是這樣的:

<bean xmlns:mvc="http://www.springframework.org/schema/mvc".... 

而且不知道爲什麼你正在嘗試使用@{..}當您指定的路徑訪問的資產。嘗試刪除@{..}像這樣:

<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> 
6

的問題是,你是混合Thymeleaf的語法@{}標準的HTML標籤href。更改如下:

<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(); 
相關問題