對不起,如果這已經被問過,但是我所嘗試過的一切都沒有奏效。我遇到的問題是,當url不是root時,我的靜態資源沒有被加載,即如果url是localhost:8080/users,那麼css文件加載正常,但如果我嘗試加載localhost的css文件:8080/users/1或任何其他網址後,用戶不加載靜態文件。我的靜態文件在resources/static/public/css中。當url不是root的時候在Spring Boot中訪問靜態資源
我知道這可能是一些愚蠢的東西。提前致謝。
我webConfig類:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/webjars/**").permitAll();
http
.authorizeRequests()
.antMatchers("/",
"/home",
"/error",
"/signup",
"/forgot-password",
"/reset-password/*",
"/public/**").permitAll()
.anyRequest().authenticated();
http
.formLogin()
.loginPage("/login")
//.defaultSuccessUrl()
.permitAll().and()
.rememberMe().key(rememberMeKey).rememberMeServices(rememberMeServices()).and()
.logout()
.permitAll();
}
MvcConfig類
@Configuration
@ComponentScan
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
registry.addViewController("/login").setViewName("login");
}
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
}
更新*** 頭文件
<link rel="shortcut icon" type="image/png" sizes="16x16" href="./public/images/favicon/logo.png"/>
<link type="text/css" rel="stylesheet" href="./public/css/style.css"/>
頁腳:
<script src="./public/lib/bootstrap-3.1.1/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="./public/js/home.js"></script>
<script src="./public/js/webcam.js"></script>
<script src="./public/js/recordVideo.js"></script>
問題可能出在你在模板中包含資源的方式。請張貼。 –
只是在那裏添加它們!乾杯。我也將這部署到tomcat中,以防萬一。 – LiamMalone