Spring Boot应用程序不提供静态内容

时间:2015-03-20 17:37:20

标签: java spring spring-mvc configuration spring-boot

我正在使用Spring Boot,并尝试在部署时使我的静态资源(CSS,JS,Fonts)可用。源代码可供您查看或克隆https://github.com/joecracko/StaticResourceError

现在我的CSS,JS和Font文件对我已部署的网站不可见。

这是我的项目目录结构:

Here is my project directory structure

以下是已编译JAR的根目录:我向您保证文件存在于各自的文件夹中。

enter image description here

以下是我看到的网络错误:

enter image description here

以下是Chrome工具提供的资源。请注意,bar.css在此处显示为空。您可以查看我的源代码,看它是不是空的。

enter image description here

这是我的homepage.html

<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>

<!-- Main Styles -->
<link rel="stylesheet" href="/css/bar.css" />
<script src="/js/foo.js"></script>

</head>
<body>
    <div>Welcome to Foo!</div>
</body>
</html>

这是我的Web App Initializer (FooWebAppInitializer.java)

public class FooWebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) {

        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(ServletConfig.class);

        // Manage the lifecycle of the root application context
        container.addListener(new ContextLoaderListener(rootContext));

        //Spring Security
        container.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain"))
            .addMappingForUrlPatterns(null, false, "/*");

        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcherServlet", new DispatcherServlet(rootContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/*");
        dispatcher.addMapping("*.css");
        dispatcher.addMapping("*.eot");
        dispatcher.addMapping("*.svg");
        dispatcher.addMapping("*.ttf");
        dispatcher.addMapping("*.woff");
        dispatcher.addMapping("*.map");
        dispatcher.addMapping("*.js");
        dispatcher.addMapping("*.ico");
    }
}

这是我的Servlet配置(ServletConfig.java)

@Configuration
@EnableWebMvc
@ComponentScan({"com.foo"})
public class ServletConfig extends WebMvcAutoConfiguration{

    @Bean
    MultipartResolver multipartResolver() {
        return new StandardServletMultipartResolver();
    }

    @Bean
    public ResourceBundleMessageSource messageSource() {
        ResourceBundleMessageSource source = new ResourceBundleMessageSource();
        source.setBasename("messages");
        return source;
    }
}

对于踢,My Spring Security Config (WebSecurityConfig.java)

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .anyRequest().permitAll();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**"); // #3
    }
}

2 个答案:

答案 0 :(得分:16)

静态资源放在目录下:

/src/main/resources/static

您也可以使用publicresources代替static作为文件夹名称。

解释:您的构建工具(Maven或Gradle)将复制应用程序类路径/src/main/resources/的所有内容,并在{{3 }},类路径中名为/static(或/public/resources}的目录中的所有内容都将作为静态内容提供。


这个目录也可以,但不鼓励:

/src/main/webapp/
  

如果您的应用程序是,请不要使用src / main / webapp目录   打包成一个罐子。虽然这个目录是一个通用标准,但它   只会与战争包装一起使用,它将被默默地忽略   如果你生成一个jar,大多数构建工具。

答案 1 :(得分:1)

有两件事需要考虑(Spring Boot v1.5.2.RELEASE) - 1)检查@EnableWebMvc注释的所有Controller类,如果有则删除它 2)检查使用了注释的Controller类 - @RestController或@Controller。不要在一个类中混合Rest API和MVC行为。对于MVC,使用@Controller和REST API使用@RestController

做上述2件事解决了我的问题。现在我的spring boot正在加载静态资源,没有任何问题。 @Controller =&gt; load index.html =&gt;加载静态文件。

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />


    <link rel="stylesheet/less" th:href="@{/webapp/assets/theme.siberia.less}"/>

    <!-- The app's logic -->
    <script type="text/javascript" data-main="/webapp/app" th:src="@{/webapp/libs/require.js}"></script>
    <script type="text/javascript">
        require.config({
            paths: { text:"/webapp/libs/text" }
        });
    </script>

     <!-- Development only -->
     <script type="text/javascript" th:src="@{/webapp/libs/less.min.js}"></script>


</head>
<body>

</body>
</html>

的index.html

{{1}}
相关问题