Spring 4 - addResourceHandlers无法解析静态资源 [英] Spring 4 - addResourceHandlers not resolving the static resources

查看:799
本文介绍了Spring 4 - addResourceHandlers无法解析静态资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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

Spring 4加载静态资源

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆