![](/img/trans.png)
[英]Serving static content from Jersey resource method in a Spring Boot app
[英]Spring Boot app not serving static content
我正在使用Spring Boot,並且嘗試在部署時使我的靜態資源(CSS,JS,Fonts)可用。 您可以從https://github.com/joecracko/StaticResourceError查看或克隆源代碼。
現在我的CSS,JS和Font文件對我部署的網站不可見。
這是我的項目目錄結構:
這是已編譯JAR的根目錄:我向您保證文件存在於各自的文件夾中。
這是我看到的網絡錯誤:
以下是我的Chrome工具提供的資源。 請注意,bar.css在此處顯示為空。 您可以查看我的源代碼,看它是不是空的。
這是我的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
}
}
將靜態資源放在目錄下:
/src/main/resources/static
您還可以使用public
或resources
而不是static
作為文件夾名稱。
說明 :您的構建工具(Maven或Gradle)將復制應用程序類路徑中 /src/main/resources/
所有內容,並且如Spring Boot的文檔中所述 ,從名為/static
(或/public
或)的目錄中復制所有內容。類路徑中的/resources
)將作為靜態內容提供。
這個目錄也可以,但不鼓勵:
/src/main/webapp/
如果您的應用程序將打包為jar,請不要使用src / main / webapp目錄。 雖然這個目錄是一個通用標准,但它只適用於war包裝,如果你生成一個jar,它將被大多數構建工具默默忽略。
有兩件事需要考慮(Spring Boot v1.5.2.RELEASE) - 1)檢查@EnableWebMvc注釋的所有Controller類,如果有則刪除它2)檢查使用了注釋的Controller類 - @RestController或@Controller 。 不要在一個類中混合Rest API和MVC行為。 對於MVC,使用@Controller和REST API使用@RestController
做上述兩件事解決了我的問題。 現在我的spring boot正在加載靜態資源,沒有任何問題。 @Controller => load index.html =>加載靜態文件。
@Controller
public class WelcomeController {
// inject via application.properties
@Value("${welcome.message:Hello}")
private String message = "Hello World";
@RequestMapping("/")
public String home(Map<String, Object> model) {
model.put("message", this.message);
return "index";
}
}
的index.html
<!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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.