繁体   English   中英

Spring 4 - addResourceHandlers 不解析静态资源

[英]Spring 4 - addResourceHandlers not resolving the static resources

我的maven spring项目目录结构如下图。 我正在使用基于 Spring-4 注释的配置。 我配置资源如下。 我尝试了许多 Stackoverflow 问题和其他网站中建议的方法

Spring 4 加载静态资源

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 不支持 servlets 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">

我想这有点晚了,但是我最近遇到了类似的问题。 折腾了几天,终于发现我的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("/app") 类,它有一个 @RequestMapping("/about") 方法,那么 data/app/about 和 content/app/about 都将只调用该方法(并且没有实际尝试,我想我也可能以 /app/courses/whatEverPath 的形式访问资源),因为调度程序会同时侦听“data/”和“content/”,并且仅分析 url 的其余部分(“app/about " 在这两种情况下)找到合适的@Controller。

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

这对我有用。 文件位于/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/");
    }
}

我正在使用 Spring Boot 2.2.1 和“spring-boot-starter-web”和“spring-boot-starter-tomcat”。 在我的情况下,当我使用空的“类路径:/ ”时,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

可以简化网页的 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 中提取的任何文件名连接起来,以标识资源的实际位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM