2015-11-25 301 views
2

你好,我試圖呈現我的HTML頁面thymeleaf但它無法加載.js文件。我使用的是spring-boot,我的文件結構是src/main/resources/templates,它包含我所有的html文件。我的css文件位於src/main/resources/static/css/。我的.js文件位於src/main/resources/js/Thymeleaf無法加載資源

現在我試圖使我的guest.html文件,但它的失敗,因爲我試圖檢查對鉻元素加載資源,並顯示以下錯誤消息:

http://localhost:8080/js/bootstrap-formhelpers.min.js Failed to load resource: the server responded with a status of 404 (Not Found) 
http://localhost:8080/js/bootstrap.min.js Failed to load resource: the server responded with a status of 404 (Not Found) 

下面是我guest.html問題文件無法找到資源:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" th:src="@{https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js}" ></script> 
<script src="../js/bootstrap.min.js" th:src="@{js/bootstrap.min.js}"></script> 
<script src="../js/bootstrap-formhelpers.min.js" th:src="@{js/bootstrap-formhelpers.min.js}"></script> 
+0

在旁註中,當使用'@ {...}'鏈接url表達式時,您應該始終使用'/'開始您的資源URL。這確保了Thymeleaf將創建一個從上下文路徑開始的鏈接。 –

回答

5

默認情況下,Spring Boot不提供src/main/resources的靜態內容。這從以下類路徑位置服務內容:

  • classpath:/META-INF/resources
  • classpath:/static
  • classpath:/resources
  • classpath:/public

爲此,src/main/resources/static/css送達,但不src/main/resources/js/

js目錄移動到src/main/resources/static目錄以使其投入使用。另一種方法是增加一個資源處理程序:

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
    registry.addResourceHandler("/js/**") 
      .addResourceLocations("classpath:/js/"); 
} 

您也可以覆蓋spring.resources.staticLocations財產。

有關使用Spring Boot提供靜態內容的更多信息,請參見Spring Boot Documentation: Static Content