zoukankan      html  css  js  c++  java
  • spring4静态资源获取

    Spring4做项目时,发现图片等静态资源获取不到,网上查询结果几乎相同,就是修改spring-servlet.xml配置,试了之后发现问题并不能解决,最后查看spring文档中的HTTP caching support for static resources,在项目web包里添加一个WebConfig.java文件,问题成功解决。

    以下是WebConfig.java代码。

    package com.smart.web;//修改为自己的目录
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.ViewResolver;
    import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    
    
    @Configuration
    @EnableWebMvc
    @ComponentScan("com.smart.web")   //修改为自己的目录
    public class WebConfig extends WebMvcConfigurerAdapter {
    
    
        @Bean
        public ViewResolver viewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");
            return resolver;
        }
    
        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/img/**")
                    .addResourceLocations("/static/images/");
        }
    }
    

      以下贴出我的项目目录。

    在jsp页面中如需访问图片,实例

    <img src="img/65186-106.jpg" alt="第三张">。

    图片获取成功,问题解决。
  • 相关阅读:
    js 自定义方法 实现停留几秒 sleep
    java 通过sftp服务器上传下载删除文件
    iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
    iOS JavaScriptCore使用
    UIWebView和WKWebView的使用及js交互
    WKWebView的新特性与使用
    OC与JS的交互详解
    iOS开发支付集成之微信支付
    iOS开发之支付宝集成
    React-Native学习指南
  • 原文地址:https://www.cnblogs.com/youth-dream/p/7232764.html
Copyright © 2011-2022 走看看