Including and storing resource and media files like css, img file etc

Previously I have faced the situation that I was trying to add resources and images in my maven project and was dwelling on how to add and use those files within my project.  After a long search i have come across with a solution reading blogs, stackoverflow and maven’s official web site

As you know we have three folder schemes in a maven project


Based on the schema we keep our java source codes underneath java folder, and webapp involves our web pages and configuration files such as xmls, and what we misunderstand is resources folder which we should never use to keep our files. To solve the issue follow my lead

First of all create a folder or folders to keep your files, you may create individual foldes such as css for css files images for images etc. right under your folder webapp check the image below


locate your resource folders right underneath the webapp folder and lets switch to the spring configuration file in this case mine is called as dispatcher-servlet.xml. add the lines below, to defer frictions I am giving the full code of the xml config file

<?xml version="1.0" encoding="windows-1252"?>
<beans xmlns=""

	<context:annotation-config />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/images/**" location="/images/" />
		<context:component-scan base-package="com.akbank.controller" />

			<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            	<property name="prefix">
                <property name="suffix">

As you see I added a resource mapping to map my resource files, in your view you will be using the code just like this an image and a css file import given in these instructions:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" href="css/bootstrap.css" type="text/css">

<title>Maven Resource Example</title>

<img src="images/cartref.png"/>

	<button type="button" class="btn btn-info">Info</button>

If you experience issues in spring configuration file when you add the code make sure you are adding bean xml schema especially mvc one

Leave a Reply

Your email address will not be published. Required fields are marked *