Book Image

Spring MVC Beginner's Guide

By : Amuthan Ganeshan
Book Image

Spring MVC Beginner's Guide

By: Amuthan Ganeshan

Overview of this book

Table of Contents (19 chapters)
Spring MVC Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – serving static resources


Let's see how we can serve static images with Spring MVC:

  1. Place some images under the src/main/webapp/resources/images/ directory; I have used three product images, namely P1234.png, P1235.png, and P1236.png.

  2. Add the following tag in our web application context's configuration DispatcherServlet-context.xml file:

    <mvc:resources  location="/resources/"  mapping="/resource/**"/>
  3. Now, run our application and enter http://localhost:8080/webstore/resource/images/P1234.png (change the image name in the URL based on the images you placed in step 1).

  4. You are now able to view the image you requested in the browser.

What just happened?

What just happened was simple; in step 1, we placed some image files under the src/main/webapp/resources/images/ directory. In step 2, we just added the <mvc:resources> tag in the web application context configuration to tell Spring where these image files are located in our project so that spring can serve those files...