如何在 Spring 3.2 中映射静态资源进行 javaconfig



我有下一个项目结构:

+MyApp
|
+-+src
  |
  +-+main 
    |
    +-> java
    |
    +-> resources
    |
    +-+ webapp
      |
      +-> resources ( img, css, js)
      |
      +-> WEB-INF ( views )

我的配置文件是

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.example.spring"})
public class WebConfig extends WebMvcConfigurerAdapter
{
   ...
   @Override
   public void addResourceHandlers(ResourceHandlerRegistry registry) 
   {
     registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
   }
   ...
}

当我尝试加载css文件()时,我会得到一些.css找不到。

我试图构建 https://github.com/spring-guides/tut-web.git(步骤6)。它与静态资源具有相同的问题。

如何解决这个问题?

我已经找到了问题的答案。

我使用百里香叶模板引擎。在这种情况下,我需要编写下一个:

<!DOCTYPE html>
<html xmlns:th="http://thymeleaf.org">
<html>
   <head>
     <link th:href="@{/resources/css/some.css}" rel="stylesheet" />
   </head>
   <body>
     <img th:src="@{/resources/img/some.jpg}" />
   </body>
</html>

该构造将创建网址,如网址 + Web 应用名称 + 静态资源的 uri

最新更新