Spring MVC显示图像
问题描述:
我想在我的Spring MVC Project的JSP页面中显示图像,但图像不起作用。Spring MVC显示图像
的图像放在src /主/ web应用/资源/ images文件夹
请找到下面的代码。任何帮助将不胜感激:
@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>
答
基于我们的文件夹结构资源映射改变
@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/");
}
答
给定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
我已经编辑你的建议,但仍然没有运气@覆盖 \t公共无效addResourceHandlers(ResourceHandlerRegistry注册表){ \t \t registry.addResourceHandler( “/资源/ **”)。addResourceLocations( “/资源/”) ; \t \t \t} – astar
一次清洁,建立自己的源代码 –
完成,但没有运气还是 – astar