春季4 – addResourceHandlers不parsing静态资源

下面显示了我的maven spring项目目录结构。 我正在使用Spring-4基于注释的configuration。 我configuration下面的资源。 我尝试了许多Stackoverflow问题和其他网站的build议

Spring 4加载静态资源

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不支持servlet 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"> 

我猜这有点晚了,不过最近我还面临类似的问题。 经过几天的摔跤,最后事实certificate,我的DispatcherServlet没有被configuration为处理请求,因此资源从未被查找过。 所以我希望别人会觉得这个答案有用。

如果你给你的configuration类的调度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应该评估这个调用,而内容/告诉servlet资源处理器是“controller”。

但是我想在我的前端(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都会调用这个方法而没有真正尝试我想我可能会访问资源作为/应用程序/ courses / whatEverPath),因为调度程序同时收听“数据/”和“内容/”,并分析只剩下的url(“应用/关于“在这两种情况下)find正确的@Controller。

无论如何,目前我所达到的解决scheme对我来说已经足够令人满意了,所以我会保持原样。

这对我有效。 文件位于/resources/js/select.js 。 注意你不会缺less@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/"); } } 

可以简化网页的URI来包含资源的文件名:
<link href="bootstrap.css" rel="stylesheet" media="screen">
适当的configuration可能如下:

 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("*.css").addResourceLocations("/resources/css/"); } 

Spring将'/ resources / css /'string与从URI中提取的任何文件名连接起来,以标识资源的实际位置。