I'm having a hard time getting an image to display using Spring MVC with JSP. I know there are other very similar SO questions, but none of the answers on those questions seemed to work.
I have a very simple project with only two java files and a single JSP file to display. This is my WebMvcConfigurerAdapter class:
@Configuration
@ComponentScan(basePackages="com.rigatron.rigs4j")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
This is my home page's controller class:
@Controller
public class HomeController {
@RequestMapping(value="/")
public ModelAndView test(HttpServletResponse response) throws IOException {
return new ModelAndView("home");
}
Here is the line in the JSP file where I am trying to display my image:
<img src="/resources/old.jpeg" alt="Photo of Youthful William" id="pic" />
I have old.jpeg in my /resources/ folder. The rest of the page is displayed fine, but only the alt text is displayed here.
I am deploying this app using Tomcat8, and I have this line is my Maven pom file which copies the produced WAR file into Tomcat's webapps directory every time I build it:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<outputDirectory>C:\my\local\dir\tomcat8\webapps</outputDirectory>
</configuration>
</plugin>
So when I navigate using my browser to localhost:8080/myprojectname the rest of the JSP file is displayed correctly, but the photo is not, only the alt text. Any help with this issue would be appreciate.