Spring Boot应用程序不提供静态内容 [英] Spring Boot app not serving static content
问题描述
我正在使用Spring Boot,并试图在部署时使我的静态资源(CSS,JS,字体)可用.源代码可供您查看或克隆 https://github.com/joecracko/StaticResourceError.
I am using Spring Boot, and am trying to make my static resources (CSS, JS, Fonts) available when deployed. The source code is available for you to look at or clone from https://github.com/joecracko/StaticResourceError.
现在,我的CSS,JS和字体文件对我部署的网站不可见.
Right now my CSS, JS, and Font files are not visible to my deployed website.
这是我的项目目录结构:
这是已编译的JAR的根目录:我向您保证文件位于各自的文件夹中.
Here is the root directory of the compiled JAR: I assure you the files are present in their respective folders.
这是我看到的网络错误:
这是我的Chrome工具提供的资源.请注意,bar.css在此处显示为空.您可以查看我的源代码,以了解它不是空的.
And here are my sources provided by chrome tools. Note that bar.css appears empty here. You may look at my source code to see that it is not empty.
这是我的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应用初始化器(FooWebAppInitializer.java)
Here is my 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)
Here is my Servlet Configuration (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;
}
}
我的Spring Security Config (WebSecurityConfig.java)
And for kicks, 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
}
}
推荐答案
在目录下放置静态资源:
/src/main/resources/static
您也可以使用public
或resources
代替static
作为文件夹名称.
You can also use public
or resources
instead of static
as folder name.
说明:您的构建工具(Maven或Gradle)将复制应用程序 classpath 中/src/main/resources/
中的所有内容,并按照
Explanation: your build tool (Maven or Gradle) will copy all the content from /src/main/resources/
in the application classpath and, as written in Spring Boot's docs, all the content from a directory called /static
(or /public
or /resources
) in the classpath will be served as static content.
此目录也可以使用,但不建议使用:
This directory could works also, but it is discouraged:
/src/main/webapp/
如果您的应用程序将使用src/main/webapp目录,请不要使用 包装成罐子.尽管此目录是一个通用标准,但它 仅适用于战争包装,将被沉默地忽略 大多数生成工具(如果您生成jar的话).
Do not use the src/main/webapp directory if your application will be packaged as a jar. Although this directory is a common standard, it will only work with war packaging and it will be silently ignored by most build tools if you generate a jar.
这篇关于Spring Boot应用程序不提供静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!