首先我在RootConfig中使用了WebMvcConfigurerAdapter,并且連接了資源,但是我沒有部署Spring Security專案。
@Configuration
@ComponentScan({"org.example"})
public class RootConfig extends WebMvcConfigurerAdapter {
}
然后我從 RootConfig 類中洗掉了 WebMvcConfigurerAdapter,專案變得可部署,但資源停止作業。
@Configuration
@ComponentScan({"org.example"})
public class RootConfig {
}
專案結構
我如何連接樣式:
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/index.css">
我的資源處理程式:
@Configuration
@EnableWebMvc
@ComponentScan({
"org.example",
"org.example.model",
"org.example.repo",
"org.example.service"
})
public class WebConfig implements WebMvcConfigurer {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
SpringMvcDispatcherServletInitializer:
public class SpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
uj5u.com熱心網友回復:
使用此語法訪問您的 css 檔案 <link href="<c:url value="/resources/css/index.css />" rel="stylesheet">
uj5u.com熱心網友回復:
嘗試使用 web-security 在如下資源上覆寫配置方法,如果您正在使用 thymleaf,請使用 @ 來表示 URI href="@{/**/css/index.css}"或正確檢查路徑。
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/*");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/336459.html
上一篇:DPAPI輸出緩沖記憶體管理
下一篇:信封加密有什么好處?