36

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 loading static resources

http://imwill.com/spring-mvc-4-add-static-resources-by-annotation/#.U5GZlXKs9i4

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">

EDIT: 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?
     }

}

Project structure

4

5 回答 5

23

这有效,

   registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");

在 jsp 文件中,我提到了静态资源,例如

<link href="resources/css/bootstrap.css" rel="stylesheet" media="screen">
于 2014-08-01T03:22:59.353 回答
13

我想这有点晚了,但是我最近遇到了类似的问题。经过几天的折腾,终于发现我的 DispatcherServlet 没有配置为处理请求,因此从未查找过资源。所以我希望其他人会发现这个答案很有用。

如果您在上面为您的配置类提供的调度程序 servlet 不是映射到根(“/”)而是映射到顶部单词(例如“/data/”),那么您可能会面临同样的问题。

假设我的调度程序 servlet 有一个映射为“/data/*”。所以我的电话看起来像

http://localhost:8080/myWebAppContext/data/command

我认为如果我有一个资源映射,例如“/content/**/*”,那么我可以访问它

http://localhost:8080/myWebAppContent/content/resourcePath

但这不是真的,我应该使用

http://localhost:8080/myWebAppContent/data/content/resourcePath

反而。这对我来说不是很清楚,而且由于大多数示例使用根“/”作为调度程序 servlet 的映射,因此这不是问题。稍后考虑,我应该更早知道 - /data/ 告诉 DispatcherServlet 应该评估调用,而 content/ 告诉 servlet 资源处理程序是“控制器”。

但是我想在我的前端(angularJs)中非常清楚地说明我是在寻找数据(通过 REST 服务)还是寻找内容(返回纯文本)。数据来自数据库,但内容来自文件(例如 pdf 文档)。因此,我决定向调度程序 servlet 添加两个映射:

public class MidtierWebConfig implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(MidtierAppConfig.class);

    servletContext.addListener(new ContextLoaderListener(rootContext));

    AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
    dispatcherContext.register(MidtierDispatcherConfig.class);

    Dynamic netskolaDispatcher = servletContext.addServlet(
        "dispatcher",
        new DispatcherServlet(dispatcherContext)
    );
    netskolaDispatcher.setLoadOnStartup(1);
    netskolaDispatcher.addMapping("/data/*");
    netskolaDispatcher.addMapping("/content/*");
}

}

MidtierAppConfig 类为空,但 M​​idtierDispatcherConfig 定义了静态资源:

@Configuration
@ComponentScan("my.root.package")
@EnableWebMvc
public class MidtierDispatcherConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
            .addResourceHandler("/courses/**/*")
            .addResourceLocations("/WEB-INF/classes/content/")
        ;
    }
}

现在,当我想访问我的@Controller 时,我使用 /data/ 前缀,而当我想访问我的资源时,我使用 /content/ 前缀。需要注意的是,如果我有一个具有 @RequestMapping("/about") 方法的 @RequestMapping("/app") 类,那么 data/app/about 和 content/app/about 都将调用该方法(并且没有实际尝试,我想我也可能以 /app/courses/whatEverPath 的形式访问资源),因为调度程序同时监听“data/”和“content/”,并且只分析 url 的其余部分(“app/about " 在这两种情况下) 来找到正确的@Controller。

无论如何,我目前达到的解决方案对我来说已经足够令人满意了,所以我会保持原样。

于 2014-09-06T23:33:40.467 回答
8

这对我有用。文件可在 /resources/js/select.js. 请注意,您不会缺少@EnableWebMvc注释....

@EnableWebMvc
@EnableTransactionManagement
public class ApplicationContextConfig extends WebMvcConfigurerAdapter {

    @Bean(name = "viewResolver")
    public InternalResourceViewResolver getViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
        .addResourceLocations("/resources/");
    }
}
于 2015-06-05T09:51:11.503 回答
3

我正在使用 Spring Boot 2.2.1 和“spring-boot-starter-web”和“spring-boot-starter-tomcat”。在我的情况下,当我使用空的“ classpath:/ ”时,Spring Boot 只能找到“ /resources/ ”文件夹

我的文件夹结构:

在此处输入图像描述

我的代码:

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
        .addResourceHandler("/resources/**")
        .addResourceLocations("classpath:/");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }
}

使用浏览器,我可以找到任何文件,例如:

http://localhost:8080/resources/test.txt

http://localhost:8080/resources/images/background-1.png

于 2020-02-23T02:32:07.013 回答
0

可以将网页的 URI 简化为仅包含资源的文件名:
<link href="bootstrap.css" rel="stylesheet" media="screen">
适当的配置可能如下所示:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("*.css").addResourceLocations("/resources/css/");
}

Spring 将 '/resources/css/' 字符串与从 URI 中提取的任何文件名连接起来,以识别资源的实际位置。

于 2016-02-29T15:46:53.397 回答