2017-10-12 152 views
0

我想在我的Spring MVC Project的JSP頁面中顯示圖像,但圖像不起作用。Spring MVC顯示圖像

的圖像放在src /主/ web應用/資源/ images文件夾

Folder structure

請找到下面的代碼。任何幫助將不勝感激:

@Configuration 
@EnableWebMvc 
public class WebMVCConfig extends WebMvcConfigurationSupport { 

// Resource reading from jsp 

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 

//Here I declare a mapping of src\main\webapp\resources folder and all its content to a resource location value /resources/ 
registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/"); 

} 


// Property reading from jsp 
@Bean 
public MessageSource messageSource() { 
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); 
    messageSource.setBasename("/WEB-INF/i18n/application"); 
    return messageSource; 
} 


// View Resolver 
@Bean 
public ViewResolver viewResolver() { 
    InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver(); 
    //internalResourceViewResolver.setViewClass(JstlView.class); 
    internalResourceViewResolver.setPrefix("/"); 
    internalResourceViewResolver.setSuffix(".jsp"); 
    return internalResourceViewResolver; 
} 

}

在JSP中它被稱爲:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

<html> 
<head> 
</head> 
<body> 
<img src="<c:url value='/resources/images/abc.jpg' />" > 
</body> 
</html> 

回答

0

基於我們的文件夾結構資源映射改變

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 

//Here I declare a mapping of src\main\webapp\resources folder and all its 
    content to a resource location value /resources/ 
    registry.addResourceHandler("/resources/**") 
    .addResourceLocations("/resources/"); 

} 
+0

我已經編輯你的建議,但仍然沒有運氣@覆蓋 \t公共無效addResourceHandlers(ResourceHandlerRegistry註冊表){ \t \t registry.addResourceHandler( 「/資源/ **」)。addResourceLocations( 「/資源/」) ; \t \t \t} – astar

+0

一次清潔,建立自己的源代碼 –

+0

完成,但沒有運氣還是 – astar

0

給定t,ResourceResolvers可以解析資源繼承人URL路徑。考慮到它們的內部資源路徑,它們還可以解析客戶端使用的面向外部的公用URL路徑。

XML配置:

<mvc:resources mapping="/resources/**" location="/resources/"/> 

註釋配置:

使用jsp中:

<img src="/contextpath/resources/images/abc.jpg" > 

資源文件夾結構工程:

TestProject 
| 
src 
| 
WebContent 
     | 
      resources 
        | 
        js/css/images 
+0

現在解決了,謝謝 – astar