2015-11-25 397 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