3

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.

1 Answer 1

2

Ok, I figured out where I was going wrong. This SO post pointed me in the right direction:

Can't display images in JSP

So basically, I was trying to load resources from the root of the web server instead of the root of my webapp. So, in order to reference the root of the webapp I included these two lines:

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

...

<img src="<c:url value='/resources/old.jpeg'/>" alt="Photo of Youthful William" id="pic" />

In order to use this JSTL tag I had to include this Maven dependency:

<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

I apologize for perhaps posting this question prematurely, but I'm going to leave it up in hopes that it can help someone else.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.