【发布时间】:2014-07-31 14:16:42
【问题描述】:
我的maven spring项目目录结构如下图。我正在使用基于 Spring-4 注释的配置。我配置如下资源。我尝试了许多 Stackoverflow 问题和其他网站中建议的方法
Spring 4 loading static resources
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?
}
}
【问题讨论】:
-
您需要预先添加您的 Web 应用程序的上下文路径。
-
但是,这不会对上下文进行硬编码吗?我想知道它如何使用相同的配置,但在 xml 配置中,spring-3.2.6!
-
你不会硬编码任何东西:stackoverflow.com/questions/12705193/…
标签: java spring spring-mvc spring-annotations spring-4