无法加载资源:服务器响应状态为404

时间:2015-12-16 19:42:41

标签: image maven spring-mvc

我有一个应用程序(使用注释的Spring 4 MVC + Hibernate 4 + MySQL + Maven集成示例),使用基于注释的配置将Spring与Hibernate集成。 我有一个具有这种结构的maven项目:

enter image description here

我希望通过home.png访问http://xxx.xxx.221.107:8080/myApp/images/home.png图片,但我得到了404

拉开战争图像就在那里: myapp / images / home.png

我也尝试过这样做:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"fr.devices"})
public class AppConfig {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }


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


    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }
}

将所有静态文件移动到/ webapp / resources / 并从jsp

<img src="${pageContext.servletContext.contextPath}/resources/images/home.png" width="136" height="43"   /> 

具有相同的结果

1 个答案:

答案 0 :(得分:0)

请添加资源处理程序配置。

例如; Java配置

public class Config extends WebMvcConfigurerAdapter{
   @Override
   public void addResourceHandlers(ResourceHandlerRegistry registry) {  
    registry.addResourceHandler("/images/**").addResourceLocations("/images/");
   }
}

或xml

 <mvc:resources mapping="/images/**" location="/images/"  cache-period="31556926"/>
JSP中的

 <c:set var="cp" value="${pageContext.request.servletContext.contextPath}" scope="request"></c:set>
 <img src="<c:url value="/images/home.png">" width="136" height="43"  />