Spring 4 - addResourceHandlers 不解析静态资源 [英] Spring 4 - addResourceHandlers not resolving the static resources
问题描述
我的maven spring项目目录结构如下图.我正在使用基于 Spring-4 注释的配置.我配置如下资源.我尝试了许多 Stackoverflow 问题和其他网站中建议的方法
My maven spring project directory structure is shown below. I am using Spring-4 annotation based configuration. I configure the resources like below. I tried many ways that are suggested in many Stackoverflow questions and other websites
http://imwill.com/spring-mvc-4-add-static-resources-by-annotation/#.U5GZlXKs9i4
但是jsp文件无法加载资源,所有静态内容请求都返回404错误.我在jsp中尝试过这些东西,
But the jsp files could not load the resources, all the static content requests returns 404 error. I tried these things in 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,这有关系吗?
I am using servlet 2.5 because as of now I cannot upgrade my project from JBoss 5 to higher versions. JBoss5 do not support servlets 3, and do that matter?
@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文件中我引用了像
and in the jsp files I referred to the static resources like
<link href="resources/css/bootstrap.css" rel="stylesheet" media="screen">
这篇关于Spring 4 - addResourceHandlers 不解析静态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!