我的maven spring项目目录结构如下所示。我正在使用基于Spring-4注释的配置。我按如下方式配置资源。我尝试了许多Stackoverflow问题和其他网站中建议的许多方法
但是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">