我的maven spring项目目录结构如下所示。我正在使用基于 Spring-4 注释的配置。我如下配置资源。我尝试了许多 Stackoverflow 问题和其他网站中建议的方法
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">
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)