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 http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

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

src/main/java
src/main/resources
src/main/webapp

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

mavenresourcesfolders

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="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<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">
                	<value>/WEB-INF/pages/</value>
                </property>
                <property name="suffix">
                    <value>.jsp</value>
                </property>
            </bean>
</beans>

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"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<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>

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

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

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 *