[英]Spring - addResourceHandlers not resolving the static resources with Rest Controller
[英]Spring 4 - addResourceHandlers not resolving the static resources
我的maven spring項目目錄結構如下圖。 我正在使用基於 Spring-4 注釋的配置。 我配置資源如下。 我嘗試了許多 Stackoverflow 問題和其他網站中建議的方法
http://imwill.com/spring-mvc-4-add-static-resources-by-annotation/#.U5GZlXKs9i4
但是jsp文件無法加載資源,所有靜態內容請求都返回404錯誤。 我在jsp中嘗試過這些東西,
<link href="resources/css/bootstrap.css" rel="stylesheet" media="screen">
<link href="/resources/css/bootstrap.css" rel="stylesheet" media="screen">
<link href="css/bootstrap.css" rel="stylesheet" media="screen">
編輯:我正在使用 servlet 2.5,因為到目前為止我無法將我的項目從 JBoss 5 升級到更高版本。 JBoss5 不支持 servlets 3,這有關系嗎?
@Configuration
@ComponentScan("com.mgage.mvoice")
public class MyAppWebConfig extends WebMvcConfigurerAdapter {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// I tried these many combinations separately.
ResourceHandlerRegistration resourceRegistration = registry
.addResourceHandler("resources/**");
resourceRegistration.addResourceLocations("/resources/**");
registry.addResourceHandler("/css/**").addResourceLocations("/css/**");
registry.addResourceHandler("/img/**").addResourceLocations("/img/**");
registry.addResourceHandler("/js/**").addResourceLocations("/js/**");
registry.addResourceHandler("/resources/**")
.addResourceLocations("classpath:/resources/");
// do the classpath works with the directory under webapp?
}
}
這有效,
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
在jsp文件中我提到了靜態資源,比如
<link href="resources/css/bootstrap.css" rel="stylesheet" media="screen">
我想這有點晚了,但是我最近遇到了類似的問題。 折騰了幾天,終於發現我的DispatcherServlet沒有配置處理請求,所以一直沒有查資源。 所以我希望其他人會發現這個答案很有用。
如果您在上面提供的配置類的調度程序 servlet 不是映射到根(“/”)而是映射到頂部單詞(例如“/data/”),那么您可能會面臨同樣的問題。
假設我的調度程序 servlet 有一個映射為“/data/*”。 所以我的電話看起來像
http://localhost:8080/myWebAppContext/data/command
我認為如果我有一個資源映射,例如“/content/**/*”,那么我可以訪問它
http://localhost:8080/myWebAppContent/content/resourcePath
但這不是真的,我應該使用
http://localhost:8080/myWebAppContent/data/content/resourcePath
反而。 這對我來說並不清楚,而且由於大多數示例使用根“/”作為調度程序 servlet 的映射,因此這不是問題。 稍后考慮時,我應該早點知道它 - /data/ 告訴 DispatcherServlet 應該評估調用,而 content/ 告訴 servlet 資源處理程序是“控制器”。
但我想在我的前端 (angularJs) 中非常清楚我是查找數據(通過 REST 服務)還是內容(返回純文本)。 數據來自數據庫,但內容來自文件(例如 pdf 文檔)。 因此,我決定向調度程序 servlet 添加兩個映射:
public class MidtierWebConfig implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(MidtierAppConfig.class);
servletContext.addListener(new ContextLoaderListener(rootContext));
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(MidtierDispatcherConfig.class);
Dynamic netskolaDispatcher = servletContext.addServlet(
"dispatcher",
new DispatcherServlet(dispatcherContext)
);
netskolaDispatcher.setLoadOnStartup(1);
netskolaDispatcher.addMapping("/data/*");
netskolaDispatcher.addMapping("/content/*");
}
}
MidtierAppConfig 類為空,但 MidtierDispatcherConfig 定義了靜態資源:
@Configuration
@ComponentScan("my.root.package")
@EnableWebMvc
public class MidtierDispatcherConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/courses/**/*")
.addResourceLocations("/WEB-INF/classes/content/")
;
}
}
現在,當我想訪問我的@Controller 時,我使用 /data/ 前綴,而當我想訪問我的資源時,我使用 /content/ 前綴。 警告是,如果我有一個 @RequestMapping("/app") 類,它有一個 @RequestMapping("/about") 方法,那么 data/app/about 和 content/app/about 都將只調用該方法(並且沒有實際嘗試,我想我也可能以 /app/courses/whatEverPath 的形式訪問資源),因為調度程序會同時偵聽“data/”和“content/”,並且僅分析 url 的其余部分(“app/about " 在這兩種情況下)找到合適的@Controller。
無論如何,我所達到的當前解決方案對我來說已經足夠令人滿意了,所以我將保持原樣。
這對我有用。 文件位於/resources/js/select.js
。 請注意,您沒有遺漏@EnableWebMvc
注釋....
@EnableWebMvc
@EnableTransactionManagement
public class ApplicationContextConfig extends WebMvcConfigurerAdapter {
@Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}
我正在使用 Spring Boot 2.2.1 和“spring-boot-starter-web”和“spring-boot-starter-tomcat”。 在我的情況下,當我使用空的“類路徑:/ ”時,Spring Boot 只能找到“ /resources/ ”文件夾
我的文件夾結構:
我的代碼:
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("classpath:/");
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
}
使用瀏覽器我可以找到任何文件,如:
可以簡化網頁的 URI 以包含資源的文件名:
<link href="bootstrap.css" rel="stylesheet" media="screen">
適當的配置可能如下所示:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("*.css").addResourceLocations("/resources/css/");
}
Spring 將 '/resources/css/' 字符串與從 URI 中提取的任何文件名連接起來,以標識資源的實際位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.